
proc overlay2 { } {
	
    global over2
    global overlay_color
    global overlay_list
    set over2 .overlay2
    set overlay_color 0
    set overlay_list ""
	
	global commstring
	global enablestring insertstring overlaystring
	global overlay_string_enable
	
    if [winfo exists $over2] {destroy $over2}
		
    global env
    set env(DISPLAY) :0.1
    toplevel $over2 -width 25c -height 17c
    wm title $over2 "Overlay Field"

    label $over2.lab43 \
        -foreground #0000d0 -relief groove -text {Add Overlay Field} \
        -width 351
    label $over2.lab44 \
        -foreground #0000d0 -height 0 -relief groove \
        -text {Existing Overlay Field} -width 178

    label $over2.lab58 \
        -height 0 -text {Overlay Fields}

    listbox $over2.lis57 \
        -background white -height 226 -width 148 \
	-yscroll "$over2.scr59 set"

    scrollbar $over2.scr59 \
        -width 16 -command {$over2.lis57 yview}

    frame $over2.fra65 \
        -borderwidth 2 -height 155 -relief groove -width 180
    radiobutton $over2.fra65.rad66 \
        -text Analog -variable ov_type  -value analog  -command {select_analog2}
    radiobutton $over2.fra65.rad68 \
        -text Dive -variable ov_type  -value dive  -command {set type_entry "Dive"}
    radiobutton $over2.fra65.rad69 \
        -text Pitch -variable ov_type  -value pitch -command {set type_entry "Pitch"}
    radiobutton $over2.fra65.rad70 \
        -text Roll -variable ov_type  -value roll  -command {set type_entry "Roll"}
    entry $over2.fra65.ent78 \
        -background white -textvariable type_entry
    label $over2.lab71 \
        -text {Select type}
    label $over2.lab73 \
        -text {Line (5-20)}
    label $over2.lab74 \
        -height 0 -text {Column (1-4)}
    entry $over2.ent75 \
        -background white -textvariable ovline_entry -width 88
    entry $over2.ent76 \
        -background white -textvariable ovcol_entry -width 88
    label $over2.lab77 \
        -height 0 -text Color -width 0
    frame $over2.fra79 \
        -borderwidth 2 -height 225 -relief groove -width 160
    radiobutton $over2.fra79.rad80 \
        -text Black -variable ov_color -value black  -command {set overlay_color 0}
    radiobutton $over2.fra79.rad81 \
        -text Green -variable ov_color -value green -command {set overlay_color 2}
    radiobutton $over2.fra79.rad82 \
        -text Red -variable ov_color -value red  -command {set overlay_color 1}
    radiobutton $over2.fra79.rad83 \
        -text Cyan -variable ov_color  -value cyan  -command {set overlay_color 6}
    radiobutton $over2.fra79.rad84 \
        -text White -variable ov_color  -value white -command {set overlay_color 7}
    radiobutton $over2.fra79.rad85 \
        -text Magenta -variable ov_color -value magenta  -command {set overlay_color 5}
    radiobutton $over2.fra79.rad86 \
        -text Blue -variable ov_color  -value blue -command {set overlay_color 4}
    radiobutton $over2.fra79.rad87 \
        -text Yellow -variable ov_color -value yellow  -command {set overlay_color 3}

    # Units
    entry $over2.fra65.units -width 5 -bg white -textvariable unit

    # Button to add a field
    button $over2.but88 \
        -command {add_o_item2 $type_entry $ovline_entry $ovcol_entry $overlay_color $unit} \
        -height 0 -text Add
    button $over2.but89 \
        -command {delete_o_item2 $type_entry} -text Remove
    button $over2.but90 \
        -command {destroy .overlay2} -text Close

    # For the String from Comm Rocket Port 0 to be displayed
    entry $over2.commstring_entry \
           -background white -textvariable commstring -width 100 -state disabled -font {Halvetica 10}
    label $over2.commstring_lable \
        -height 0 -text String -width 0

    entry $over2.enablestring_entry \
           -background white -textvariable enablestring -width 100 -font {Halvetica 10}
    label $over2.enablestring_lable \
        -height 0 -text {Enable Bits} -width 0

    entry $over2.insertstring_entry \
           -background white -textvariable insertstring -width 100 -font {Halvetica 10}
    label $over2.insertstring_lable \
        -height 0 -text {Insert Text} -width 0

    entry $over2.overlaystring_entry \
           -background white -textvariable overlaystring -width 100 -state disabled -font {Halvetica 10}
    label $over2.overlaystring_lable \
        -height 0 -text {Overlay Text} -width 0

    checkbutton $over2.overlaystring_enable \
        -text {Overlay the String} -variable overlay_string_enable


    ###################
    # SETTING GEOMETRY
    ###################
    place $over2.lab43 \
        -x 20 -y 15 -width 351 -height 20 -anchor nw -bordermode ignore
    place $over2.lab44 \
        -x 390 -y 15 -width 178 -height 20 -anchor nw -bordermode ignore
    place $over2.lis57 \
        -x 395 -y 75 -width 148 -height 226 -anchor nw -bordermode ignore
    place $over2.lab58 \
        -x 390 -y 50 -anchor nw -bordermode ignore
    place $over2.scr59 \
        -x 540 -y 75 -width 16 -height 225 -anchor nw -bordermode ignore

    # Type
    place $over2.fra65 \
        -x 20 -y 75 -width 180 -height 155 -anchor nw -bordermode ignore
    place $over2.fra65.rad66 \
        -x 10 -y 50 -anchor nw -bordermode ignore
    place $over2.fra65.rad68 \
        -x 10 -y 75 -anchor nw -bordermode ignore
    place $over2.fra65.rad69 \
        -x 10 -y 100 -anchor nw -bordermode ignore
    place $over2.fra65.rad70 \
        -x 10 -y 125 -anchor nw -bordermode ignore
    place $over2.fra65.ent78 \
        -x 15 -y 15 -anchor nw -bordermode ignore
    place $over2.lab71 \
        -x 20 -y 50 -anchor nw -bordermode ignore
    place $over2.lab73 \
        -x 20 -y 245 -anchor nw -bordermode ignore
    place $over2.lab74 \
        -x 20 -y 280 -anchor nw -bordermode ignore
    place $over2.ent75 \
        -x 110 -y 245 -width 88 -height 22 -anchor nw -bordermode ignore
    place $over2.ent76 \
        -x 110 -y 280 -width 88 -height 22 -anchor nw -bordermode ignore
    place $over2.lab77 \
        -x 210 -y 50 -anchor nw -bordermode ignore
    place $over2.fra79 \
        -x 210 -y 75 -width 160 -height 225 -anchor nw -bordermode ignore

    # Color
    place $over2.fra79.rad80 \
        -x 10 -y 15 -anchor nw -bordermode ignore
    place $over2.fra79.rad81 \
        -x 10 -y 40 -anchor nw -bordermode ignore
    place $over2.fra79.rad82 \
        -x 10 -y 65 -anchor nw -bordermode ignore
    place $over2.fra79.rad83 \
        -x 10 -y 90 -anchor nw -bordermode ignore
    place $over2.fra79.rad84 \
        -x 10 -y 115 -anchor nw -bordermode ignore
    place $over2.fra79.rad85 \
        -x 10 -y 140 -anchor nw -bordermode ignore
    place $over2.fra79.rad86 \
        -x 10 -y 165 -anchor nw -bordermode ignore
    place $over2.fra79.rad87 \
        -x 10 -y 190 -anchor nw -bordermode ignore

    # Add, Remove, Close buttons
    place $over2.but88 \
        -x 325 -y 315 -anchor nw -bordermode ignore
    place $over2.but89 \
        -x 400 -y 315 -anchor nw -bordermode ignore
    place $over2.but90 \
        -x 500 -y 315 -anchor nw -bordermode ignore 

    #String from Comm 0 of the rocket port
    place $over2.commstring_entry \
        -x 90 -y 380 -anchor nw -bordermode ignore
    place $over2.commstring_lable \
        -x 5 -y 380 -anchor nw -bordermode ignore
    place $over2.enablestring_entry \
        -x 90 -y 400 -anchor nw -bordermode ignore
    place $over2.enablestring_lable \
        -x 5 -y 400 -anchor nw -bordermode ignore
    place $over2.insertstring_entry \
        -x 90 -y 420 -anchor nw -bordermode ignore
    place $over2.insertstring_lable \
        -x 5 -y 420 -anchor nw -bordermode ignore
    place $over2.overlaystring_entry \
        -x 90 -y 440 -anchor nw -bordermode ignore
    place $over2.overlaystring_lable \
        -x 5 -y 440 -anchor nw -bordermode ignore
    place $over2.overlaystring_enable \
        -x 5 -y 460 -anchor nw -bordermode ignore

    # Units	
    place $over2.fra65.units \
        -x 120 -y 50 -anchor nw -bordermode ignore

     	#---------------------------------------#      		
 	# Display the previous selection     	#
 	# Inserting data into the listbox	#	
  	#---------------------------------------#

  	set overlay_size [get_ov_count]
   	puts "overlay_size $overlay_size"  		

       for {set i 0} {$i < $overlay_size} {incr i} {
        	
        	set temp [split [get_overlay_info $i] |]
        	puts "overlay.tcl: temp is : $temp"
        	set temp_type [lindex $temp 0]
        	set temp_channel [lindex $temp 1]
        	set temp_line [lindex $temp 2]
        	set temp_column [lindex $temp 3]
        	
        	set name [get_element_name 0 0 $temp_channel]
        	
        	if {$temp_type == 0 } {set name "Ana Gesadc $temp_channel"}
        	if {$temp_type == 1 } {set name "Dive"}
        	if {$temp_type == 2 } {set name "Pitch"}
        	if {$temp_type == 3 } {set name "Roll"}
        	
      		$over2.lis57 insert end "$name l($temp_line) c($temp_column)"
               	
        }

    set commstring [get_overlay_string]
    set overlaystring [get_overlay_string_plus $enablestring $insertstring]

    set overlay_string_timer2 1
    loop_overlay_string_transmission2

}
 proc loop_overlay_string_transmission2 { } {

	global overlay_string_timer2
	global commstring overlaystring enablestring insertstring
	global over2

	
	if { ![winfo exists $over2] } {return}
	
	set commstring [get_overlay_string]
     	set overlaystring [get_overlay_string_plus $enablestring $insertstring]		
	
	after 200 [list loop_overlay_string_transmission2]
}



set ov_ana2  .analog_overlay2

proc select_analog2 { } {

   global type_entry
   global ov_ana2



   if [winfo exists $ov_ana2] { destroy $ov_ana2 }

   global env
   set env(DISPLAY) :0.1
   toplevel $ov_ana2


   label $ov_ana2.title -text "Analog Overlay"  -relief groove -fg blue
   pack $ov_ana2.title -side top  -fill x -padx 3 -pady 3




   menubutton $ov_ana2.b1 -text "1-8" -menu $ov_ana2.b1.menu
   menubutton $ov_ana2.b2 -text "9-16" -menu $ov_ana2.b2.menu
   menubutton $ov_ana2.b3 -text "17-24" -menu $ov_ana2.b3.menu
   menubutton $ov_ana2.b4 -text "25-32" -menu $ov_ana2.b4.menu
   menubutton $ov_ana2.b5 -text "33-40" -menu $ov_ana2.b5.menu
   menubutton $ov_ana2.b6 -text "41-48" -menu $ov_ana2.b6.menu



   menu $ov_ana2.b1.menu
   $ov_ana2.b1.menu add command -label "1 [get_element_name 0 0 1]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 1"}
   $ov_ana2.b1.menu add command -label "2 [get_element_name 0 0 2]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 2"}
   $ov_ana2.b1.menu add command -label "3 [get_element_name 0 0 3]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 3"}
   $ov_ana2.b1.menu add command -label "4 [get_element_name 0 0 4]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 4"}
   $ov_ana2.b1.menu add command -label "5 [get_element_name 0 0 5]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 5"}
   $ov_ana2.b1.menu add command -label "6 [get_element_name 0 0 6]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 6"}
   $ov_ana2.b1.menu add command -label "7 [get_element_name 0 0 7]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 7"}
   $ov_ana2.b1.menu add command -label "8 [get_element_name 0 0 8]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 8"}
	
	
	


   menu $ov_ana2.b2.menu
   $ov_ana2.b2.menu add command -label "9 [get_element_name 0 0 9]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 9"}
   $ov_ana2.b2.menu add command -label "10 [get_element_name 0 0 10]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 10"}
   $ov_ana2.b2.menu add command -label "11 [get_element_name 0 0 11]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 11"}
   $ov_ana2.b2.menu add command -label "12 [get_element_name 0 0 12]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 12"}
   $ov_ana2.b2.menu add command -label "13 [get_element_name 0 0 13]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 13"}
   $ov_ana2.b2.menu add command -label "14 [get_element_name 0 0 14]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 14"}
   $ov_ana2.b2.menu add command -label "15 [get_element_name 0 0 15]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 15"}
   $ov_ana2.b2.menu add command -label "16 [get_element_name 0 0 16]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 16"}

	
	

   menu $ov_ana2.b3.menu
   $ov_ana2.b3.menu add command -label "17 [get_element_name 0 0 17]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 17"}
   $ov_ana2.b3.menu add command -label "18 [get_element_name 0 0 18]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 18"}
   $ov_ana2.b3.menu add command -label "19 [get_element_name 0 0 19]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 19"}
   $ov_ana2.b3.menu add command -label "20 [get_element_name 0 0 20]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 20"}
   $ov_ana2.b3.menu add command -label "21 [get_element_name 0 0 21]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 21"}
   $ov_ana2.b3.menu add command -label "22 [get_element_name 0 0 22]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 22"}
   $ov_ana2.b3.menu add command -label "23 [get_element_name 0 0 23]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 23"}
   $ov_ana2.b3.menu add command -label "24 [get_element_name 0 0 24]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 24"}

	
	


   menu $ov_ana2.b4.menu
   $ov_ana2.b4.menu add command -label "25 [get_element_name 0 0 25]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 25"}
   $ov_ana2.b4.menu add command -label "26 [get_element_name 0 0 26]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 26"}
   $ov_ana2.b4.menu add command -label "27 [get_element_name 0 0 27]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 27"}
   $ov_ana2.b4.menu add command -label "28 [get_element_name 0 0 28]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 28"}   	
   $ov_ana2.b4.menu add command -label "29 [get_element_name 0 0 29]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 29"}
   $ov_ana2.b4.menu add command -label "30 [get_element_name 0 0 30]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 30"}
   $ov_ana2.b4.menu add command -label "31 [get_element_name 0 0 31]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 31"}
   $ov_ana2.b4.menu add command -label "32 [get_element_name 0 0 32]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 32"}


   menu $ov_ana2.b5.menu	
   $ov_ana2.b5.menu add command -label "33 [get_element_name 0 0 33]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 33"}
   $ov_ana2.b5.menu add command -label "34 [get_element_name 0 0 34]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 34"}
   $ov_ana2.b5.menu add command -label "35 [get_element_name 0 0 35]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 35"}
   $ov_ana2.b5.menu add command -label "36 [get_element_name 0 0 36]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 36"}   	
   $ov_ana2.b5.menu add command -label "37 [get_element_name 0 0 37]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 37"}
   $ov_ana2.b5.menu add command -label "38 [get_element_name 0 0 38]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 38"}
   $ov_ana2.b5.menu add command -label "39 [get_element_name 0 0 39]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 39"}
   $ov_ana2.b5.menu add command -label "40 [get_element_name 0 0 40]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 40"}

   menu $ov_ana2.b6.menu	
   $ov_ana2.b6.menu add command -label "41 [get_element_name 0 0 41]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 41"}
   $ov_ana2.b6.menu add command -label "42 [get_element_name 0 0 42]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 42"}
   $ov_ana2.b6.menu add command -label "43 [get_element_name 0 0 43]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 43"}
   $ov_ana2.b6.menu add command -label "44 [get_element_name 0 0 44]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 44"}   	
   $ov_ana2.b6.menu add command -label "45 [get_element_name 0 0 45]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 45"}
   $ov_ana2.b6.menu add command -label "46 [get_element_name 0 0 46]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 46"}
   $ov_ana2.b6.menu add command -label "47 [get_element_name 0 0 47]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 47"}
   $ov_ana2.b6.menu add command -label "48 [get_element_name 0 0 48]" \
	-command {destroy $ov_ana2 ; set type_entry  "Ana Gesadc 48"}








   # add a Close button
   button $ov_ana2.close -text Close -command {destroy $ov_ana2}
	

   # Pack things up
   pack $ov_ana2.b1 $ov_ana2.b2 $ov_ana2.b3 $ov_ana2.b4 $ov_ana2.close -side top
   	
	
}

proc add_o_item2 { type_ent line col clr un } {

	global over2  overlay_list
	set line $line
	
	#set temp [split $type_ent " "]
	#set type 	[ lindex $temp 0 ]

	
	set type [ lindex $type_ent 0 ]
	set type_num -1
	set channel -1
	
	if { $type == "Ana"  } {
		set type_num 0
		set channel [lindex $type_ent 2]
	} elseif { $type == "Dive" } {
		set type_num 1
	} elseif { $type == "Pitch" } {
		set type_num 2
	} elseif {  $type == "Roll" } {
		set type_num 3
	}
	
	
	#-----------------------------------------------#	
	# Send it to overlay page (c++ function)    	#
	# and save it to database as well           	#
	# if it returns -1, the element has already 	#
	# exist in database			  	#
	#-----------------------------------------------#	
	
	set exist [make_O_item $type_num $channel $line $col $clr $un]
	if {$exist == -1} { return }
	
	
	#-------------------------------#
	# Display it to the listbox	#
	#-------------------------------#
	
	$over2.lis57 insert end "$type_ent l($line) c($col)"
	
	# Append it to the new list
	#lappend overlay_list "$type_ent l($line) c($col)"
}


proc delete_o_item2 { type } {
 	
	global over2 overlay_list
	
	#---------------------------------------#
	# Get the index of line selected        #
	# ( start from "0" to "end" )		#
	#---------------------------------------#
	
	set selected [$over2.lis57 curselection]  	
	if {$selected == "" } {  set selected end }
	set deleted_item [$over2.lis57 get $selected]
	
	set type_num -1
	set channel -1	
	
	
	if { [lindex $deleted_item 0] == "Ana"  } {
		set type_num 0
		set channel [lindex $deleted_item 2]
	} elseif { [lindex $deleted_item 0] == "Dive" } {
		set type_num 1		  	
	} elseif { [lindex $deleted_item 0] == "Pitch" } {
		set type_num 2
	} elseif {  [lindex $deleted_item 0] == "Roll" } {
		set type_num 3
	}                                                     	
	
	set conn [split [get_overlay_channel $deleted_item] |]
	
	# Call c++ function to delete it from overlay
	delete_O_item $type_num $channel
	
	#------------------------------#
	# Delete it from the listbox   #
	#------------------------------#
	$over2.lis57 delete $selected
	
	# Delete it from the overlay_list
	#set overlay_list [lreplace $overlay_list $selected $selected]
	#puts "$overlay_list"
}
