
set SERIAL 5

proc add_input2 { sv } {

   #----------------------------------------------------------#
   # Initially the number of items in the list is 0
   #----------------------------------------------------------#
   global SERIAL

   set addin2 .add_input2
   if [winfo exists $addin2] {destroy $addin2}

   global env
   set env(DISPLAY) :0.1
   toplevel $addin2 -width 10c -height 5c
   wm title $addin2 "Add input"


   label $addin2.title -text "addin2g Input"  -relief groove -fg blue

   menubutton $addin2.subana -text  "Sub Analog"  -menu $addin2.subana.menu
   button $addin2.head -text  "Heading"  -relief flat -command {list_input2 $SERIAL 1 }
   button $addin2.depth -text  "Depth" -relief flat -command {list_input2 $SERIAL 2 }
   button $addin2.pitch -text  "Pitch"  -relief flat -command {list_input2 $SERIAL 3 }
   button $addin2.roll -text  "Roll"  -relief flat -command {list_input2 $SERIAL 4 }

   	

   #----------------------------------------------------------#
   # Create submenu for "Sub Analog"
   #----------------------------------------------------------#

   menu $addin2.subana.menu
   $addin2.subana.menu add cascade -label "1-8" \
	-menu $addin2.subana.menu.b1
   $addin2.subana.menu add cascade -label "9-16" \
	-menu $addin2.subana.menu.b2
   $addin2.subana.menu add cascade -label "17-24" \
	-menu $addin2.subana.menu.b3
   $addin2.subana.menu add cascade -label "25-32" \
	-menu $addin2.subana.menu.b4
   $addin2.subana.menu add cascade -label "33-40" \
	-menu $addin2.subana.menu.b5
   $addin2.subana.menu add cascade -label "40-48" \
	-menu $addin2.subana.menu.b6

   menu $addin2.subana.menu.b1

   $addin2.subana.menu.b1 add command -label "1 [get_element_name 0 0 1]" \
	-command {list_input2 0 1 }
   $addin2.subana.menu.b1 add command -label "2 [get_element_name 0 0 2]" \
	-command {list_input2 0 2 }
   $addin2.subana.menu.b1 add command -label "3 [get_element_name 0 0 3]" \
	-command {list_input2 0 3 }
   $addin2.subana.menu.b1 add command -label "4 [get_element_name 0 0 4]" \
	-command {list_input2 0 4 }
   $addin2.subana.menu.b1 add command -label "5 [get_element_name 0 0 5]" \
	-command {list_input2 0 5 }
   $addin2.subana.menu.b1 add command -label "6 [get_element_name 0 0 6]" \
	-command {list_input2 0 6 }
   $addin2.subana.menu.b1 add command -label "7 [get_element_name 0 0 7]" \
	-command {list_input2 0 7 }
   $addin2.subana.menu.b1 add command -label "8 [get_element_name 0 0 8]" \
	-command {list_input2 0 8 }


   menu $addin2.subana.menu.b2
   $addin2.subana.menu.b2 add command -label "9 [get_element_name 0 0 9]" \
	-command {list_input2 0 9 }
   $addin2.subana.menu.b2 add command -label "10 [get_element_name 0 0 10]" \
	-command {list_input2 0 10 }
   $addin2.subana.menu.b2 add command -label "11 [get_element_name 0 0 11]" \
	-command {list_input2 0 11}
   $addin2.subana.menu.b2 add command -label "12 [get_element_name 0 0 12]" \
	-command {list_input2 0 12 }
   $addin2.subana.menu.b2 add command -label "13 [get_element_name 0 0 13]" \
	-command {list_input2 0 13 }
   $addin2.subana.menu.b2 add command -label "14 [get_element_name 0 0 14]" \
	-command {list_input2 0 14 }
   $addin2.subana.menu.b2 add command -label "15 [get_element_name 0 0 15]" \
	-command {list_input2 0 15 }
   $addin2.subana.menu.b2 add command -label "16 [get_element_name 0 0 16]" \
	-command {list_input2 0 16 }


   menu $addin2.subana.menu.b3
   $addin2.subana.menu.b3 add command -label "17 [get_element_name 0 0 17]" \
	-command {list_input2 0 17 }
   $addin2.subana.menu.b3 add command -label "18 [get_element_name 0 0 18]" \
	-command {list_input2 0 18 }
   $addin2.subana.menu.b3 add command -label "19 [get_element_name 0 0 19]" \
	-command {list_input2 0 19 }
   $addin2.subana.menu.b3 add command -label "20 [get_element_name 0 0 20]" \
	-command {list_input2 0 20 }
   $addin2.subana.menu.b3 add command -label "21 [get_element_name 0 0 21]" \
	-command {list_input2 0 21 }
   $addin2.subana.menu.b3 add command -label "22 [get_element_name 0 0 22]" \
	-command {list_input2 0 22 }
   $addin2.subana.menu.b3 add command -label "23 [get_element_name 0 0 23]" \
	-command {list_input2 0 23 }
   $addin2.subana.menu.b3 add command -label "24 [get_element_name 0 0 24]" \
	-command {list_input2 0 24 }


   menu $addin2.subana.menu.b4
   $addin2.subana.menu.b4 add command -label "25 [get_element_name 0 0 25]" \
	-command {list_input2 0 25 }
   $addin2.subana.menu.b4 add command -label "26 [get_element_name 0 0 26]" \
	-command {list_input2 0 26 }
   $addin2.subana.menu.b4 add command -label "27 [get_element_name 0 0 27]" \
	-command {list_input2 0 27 }
   $addin2.subana.menu.b4 add command -label "28 [get_element_name 0 0 28]" \
	-command {list_input2 0 28 }   	
   $addin2.subana.menu.b4 add command -label "29 [get_element_name 0 0 29]" \
	-command {list_input2 0 29 }
   $addin2.subana.menu.b4 add command -label "30 [get_element_name 0 0 30]" \
	-command {list_input2 0 30 }
   $addin2.subana.menu.b4 add command -label "31 [get_element_name 0 0 31]" \
	-command {list_input2 0 31 }
   $addin2.subana.menu.b4 add command -label "32 [get_element_name 0 0 32]" \
	-command {list_input2 0 32 }
	
   menu $addin2.subana.menu.b5	
   $addin2.subana.menu.b5 add command -label "33 [get_element_name 0 0 33]" \
	-command {list_input2 0 33}
   $addin2.subana.menu.b5 add command -label "34 [get_element_name 0 0 34]" \
	-command {list_input2 0 34 }
   $addin2.subana.menu.b5 add command -label "35 [get_element_name 0 0 35]" \
	-command {list_input2 0 35 }
   $addin2.subana.menu.b5 add command -label "36 [get_element_name 0 0 36]" \
	-command {list_input2 0 36 }
   $addin2.subana.menu.b5 add command -label "37 [get_element_name 0 0 37]" \
	-command {list_input2 0 37 }
   $addin2.subana.menu.b5 add command -label "38 [get_element_name 0 0 38]" \
	-command {list_input2 0 38 }
   $addin2.subana.menu.b5 add command -label "39 [get_element_name 0 0 39]" \
	-command {list_input2 0 39 }
   $addin2.subana.menu.b5 add command -label "40 [get_element_name 0 0 40]" \
	-command {list_input2 0 40 }
	
   menu $addin2.subana.menu.b6	
   $addin2.subana.menu.b6 add command -label "41 [get_element_name 0 0 41]" \
	-command {list_input2  0 41}
   $addin2.subana.menu.b6 add command -label "42 [get_element_name 0 0 42]" \
	-command {list_input2 0 42}
   $addin2.subana.menu.b6 add command -label "43 [get_element_name 0 0 43]" \
	-command {list_input2 0 43 }
   $addin2.subana.menu.b6 add command -label "44 [get_element_name 0 0 44]" \
	-command {list_input2 0 44}
   $addin2.subana.menu.b6 add command -label "45 [get_element_name 0 0 45]" \
	-command {list_input2 0 45 }
   $addin2.subana.menu.b6 add command -label "46 [get_element_name 0 0 46]" \
	-command {list_input2 0 46 }
   $addin2.subana.menu.b6 add command -label "47 [get_element_name 0 0 47]" \
	-command {list_input2 0 47}
   $addin2.subana.menu.b6 add command -label "48 [get_element_name 0 0 48]" \
	-command {list_input2 0 48 }
                                                                                                                        	
	
   #----------------------------------------------------------#
   # add a Close button
   #----------------------------------------------------------#
   button $addin2.close -text Close -command {destroy .add_input2}

   #----------------------------------------------------------#
   # Pack things up				
   #----------------------------------------------------------#

   pack $addin2.title -side top  -fill x -padx 3 -pady 3
   pack $addin2.subana $addin2.head $addin2.depth $addin2.pitch $addin2.roll -side top	
   pack $addin2.close -side bottom -pady 3m


# end  of procedure add_input2   	
}



#----------------------------------------------------------#
# This procedure delete the selected item in the input listbox
# if the delete button is pressed without selecting anything, then by
# default the last item in the list will be deleted
#----------------------------------------------------------#

proc delete_input2  { selected } {

     global anaout_elem_num

      if {$selected == ""} { set selected end }
      set curline [.out_bpm2.input_text get $selected]

      #puts "input.tcl: removing this $curline"

      .out_bpm2.input_text delete $selected


      #----------------------------------------------------------#
      # UPDATING THE OUTPUT CONNECTION FOR INPUT ELEMENT
      #
      # Now, update the deletion from the Input element so that its output
      # connection to this element no longer exist
      # First, we have to get the input information our Analog Out is connected to
      #----------------------------------------------------------#

      set inp [display_output 1 3 $anaout_elem_num]

      #----------------------------------------------------------#
      # Now, if there is anything looks like 3|$anaout_elem_num, then delete
      # it from the string and then send it down and update the C++ database
      #----------------------------------------------------------#

      set inp [split $inp |]
      set surveh [lindex $inp 0]
      set elem_num [lindex $inp 1]

      #----------------------------------------------------------#
      # Now get the output connection from Input element
      #----------------------------------------------------------#
      set outp [display_output $surveh 0 $elem_num]
      set outp [split $outp |]

      set len [llength $outp]

      for {set i 0} {$i < $len} {incr i 2} {
      	
      	set j [expr {$i + 1} ]
      	
      	if { ( [lindex $outp $i] == 3 ) && ( [ lindex $outp $j ] == $anaout_elem_num ) } {
      	 	
      		#----------------------------------------------------------#
      	 	# delete the two value from list
      	 	#----------------------------------------------------------#
      	 	set outp [lreplace $outp $i $j]

      	}	
      }


      #----------------------------------------------------------#
      # Re append the list again with "|"
      #----------------------------------------------------------#
      set len [ expr { [llength $outp] - 1 } ]

      set temp2 ""
      for {set i 0} {$i < $len} {incr i 1} {
      	
      	set temp [lindex $outp $i]
      	set temp2 $temp2$temp|
      	

      }	

      send_out_list_info $surveh 0 $elem_num $temp2


      #-------------------------------------------------------------------------#
      # UPDATING THE INPUT LISTBOX FOR ANALOG OUTPUT ELEMENT
      #
      #-------------------------------------------------------------------------#


        #----------------------------------------------------------#
	# delete it from the C++ database
	#----------------------------------------------------------#
 	delete_single_input $anaout_elem_num


}






#----------------------------------------------------------#
# Insert input selected to the Output BPM listbox   		
#----------------------------------------------------------#

proc list_input2 { surveh elem_num } {

      global anaout_elem_num elem_name calc default_type anaout_enable_channel enable_auto enable_graphic dig_value ana_value ana_raw offset gainvalue maxautocalibrate minautocalibrate graph_type color xpos ypos vga			
      global SERIAL

      #----------------------------------------------------------#
      # We must first check if there is an input selected previously
      # inside the input listbox or not.
      # If there is one, we have to DELETE it first and update the
      # output listbox from the input element (BPM) it's connecting to
      #----------------------------------------------------------#

      set count [.out_bpm2.input_text index end]

      if { $count != 0 } {
      	delete_input2 $anaout_elem_num
      }

      #----------------------------------------------------------#
      # We're always inserting it in the beginning because we can
      # only have one input element 				    	
      #----------------------------------------------------------#

      .out_bpm2.input_text delete 0

      #----------------------------------------------------------#
      # NOTE : For serial port output, the "surveh" = 5 and for
      # Heading -> "elem_num" = 1
      # Depth -> "elem_num" = 2
      # Pitch -> "elem_num" = 3
      # Roll -> "elem_num" = 4
      #----------------------------------------------------------#

      if {$surveh == $SERIAL} {
      	switch $elem_num {
      		
      		# Heading
      		1 { .out_bpm2.input_text insert 0 "Heading"}
      		
        	# Depth
      		2 { .out_bpm2.input_text insert 0 "Depth"}

      		# Pitch
      		3 { .out_bpm2.input_text insert 0 "Pitch"}
      		
      		# Roll
      		4 { .out_bpm2.input_text insert 0 "Roll"}
      			
      	}
      	
      	
      	#----------------------------------------------------------#
      	# Send the input list to IO database
      	#----------------------------------------------------------#
      	send_out_list_info 1 3 $anaout_elem_num "$surveh|$elem_num|"
		

      	
      	
      	
      	

      }  else {

      	#----------------------------------------------------------------#
      	# Else, this is a Sub Analog Input that the user has selected, display the
      	# input element name
      	#----------------------------------------------------------------#

      	.out_bpm2.input_text insert 0 [get_element_name $surveh 0 $elem_num]

      	#----------------------------------------------------------#
      	# Send Input to IO database -> this will update the element in database
      	#----------------------------------------------------------#
      	send_io_elem_info 1 3 $anaout_elem_num $elem_name $calc $default_type $anaout_enable_channel $enable_auto $enable_graphic $dig_value $ana_value $ana_raw $offset $gainvalue $maxautocalibrate $minautocalibrate $graph_type $color $xpos $ypos $vga			

      	#----------------------------------------------------------#
      	# Send the input list to IO database
      	#----------------------------------------------------------#
      	send_out_list_info 1 3 $anaout_elem_num "$surveh|$elem_num|"
		

      	#----------------------------------------------------------#
      	# Now, do not forget to update the input element that it has                                                #
      	# an output connection to this analog output. Update the                                                  #
      	# Surface / Sub Analog In element so that its Output listbox                                               #
      	# shows connection to this Analog Out element                                                                #
      	#----------------------------------------------------------#

      	#----------------------------------------------------------#
      	# First we want to get the existing output connection from the Input element
      	#----------------------------------------------------------#
      	set new_outp [display_output $surveh 0 $elem_num]

      	#----------------------------------------------------------#
      	# First, we need to check whether the input element
      	# "$surveh analog $elem num" exist or not, if not then just return
      	#----------------------------------------------------------#

      	if {$new_outp == -1} {
      		puts "There is no input element exist for this connection. Please create the element first"
      		return
      	}

	#----------------------------------------------------------#
      	# Now append our new connection the end of the string
      	# 3 -> Analog Out
      	# Note: new_outp is in the format "$board|elem_num|" ,
      	# therefore we do not need to put another "|" between
      	# "$new_outp" and "3|$anaout_elem_num"
      	#----------------------------------------------------------#

      	set temp 3|$anaout_elem_num|
      	set new_outp $new_outp$temp
      	#puts "new_outp is : $new_outp"

      	
      	#----------------------------------------------------------#
      	# Send the output(s) over to the Input element
      	#----------------------------------------------------------#
      	
      	send_out_list_info $surveh 0 $elem_num $new_outp


       # end else
       }

# end proc list_input2
}
