#----------------------------------------------------------------------#
# Include source files
#----------------------------------------------------------------------#
source combo2.tcl
source slide_output2.tcl



#----------------------------------------------------------------------#
# Initialize variables
#----------------------------------------------------------------------#

for {set i 1 } {$i <= 32 } {incr i } {
	set slide2_enable($i) 0
}

proc slidebar_popup2 { } {

	global slide2_enable

	#----------------------------------------------------------------------#
	# Declare variables
	#----------------------------------------------------------------------#
  
	global slide2_color
	global color2



  
	#----------------------------------------------------------------------#
	# Create a new window
	#----------------------------------------------------------------------#
	set slide2 .slide_popup2


   if [winfo exists $slide2] {destroy $slide2}
   global env
   set env(DISPLAY) :0.1
   toplevel $slide2 -width 11c -height 9c
   wm title $slide2 "Creating Slidebar"

   #----------------------------------------------------------------------#
   # Creating Widgets
   #----------------------------------------------------------------------#

    label $slide2.slidebar2_title \
        -foreground #0000d8 -height 0 -relief groove -text Slidebar \
        -width 299
    label $slide2.slide2_name_label \
        -height 0 -text Name
    label $slide2.conn_label \
        -text Connection
    label $slide2.color_label \
        -text Color

    #entry $slide2.name_entry \
        -background white -textvariable slide2_name

    # Create the listbox for slidebar names
    slide_name_combobox2

    entry $slide2.slide_connection \
    	-background white -textvariable slide2_conn
    button $slide2.arrow \
    	-command {slide_add_output2}

     # Create the combobox for color
     #slide_connection_combobox2
     slide_color_combobox2


    label $slide2.pos_label \
        -height 0 -text Position -width 0
    frame $slide2.pos_frame \
        -borderwidth 2 -height 75 -relief groove -width 125
    label $slide2.pos_frame.xlabel \
        -height 0 -text x
    label $slide2.pos_frame.ylabel \
        -text y
    entry $slide2.pos_frame.xentry \
        -background white -textvariable slide2x -width 88
    entry $slide2.pos_frame.yentry \
        -background white -textvariable slide2y -width 88
    button $slide2.add \
        -command add_slidebar2 -height 0 -text Add
    button $slide2.remove \
        -command { remove_slidebar2 $slide2_conn 2} -text Remove
    button $slide2.close \
        -command {destroy .slide_popup2} -text Close


	#----------------------------------------------------------------------#
	# Bind the motion of the mouse to display the mouse
	# coordinate at current time
	#----------------------------------------------------------------------#

    bind .ventana2 <ButtonRelease-1> {
    	global slide2x slide2y
    	#puts "%x %y"
     	set slide2x %x
     	set slide2y %y
    }	


    ###################
    # SETTING GEOMETRY
    ###################

    # Connection entry and the arrow button beside it
    place $slide2.slide_connection \
    	-x 95 -y 75 -anchor nw -bordermode ignore
    place $slide2.arrow \
	-x 243 -y 75 -width 26 -height 21 -anchor nw -bordermode ignore

    place $slide2.slidebar2_title \
        -x 10 -y 10 -width 299 -height 20 -anchor nw -bordermode ignore
    place $slide2.slide2_name_label \
        -x 10 -y 45 -anchor nw -bordermode ignore
    place $slide2.conn_label \
        -x 10 -y 75 -anchor nw -bordermode ignore
    place $slide2.color_label \
        -x 10 -y 105 -anchor nw -bordermode ignore
    #place $slide2.name_entry \
        -x 95 -y 45 -anchor nw -bordermode ignore
    place $slide2.pos_label \
        -x 10 -y 140 -anchor nw -bordermode ignore
    place $slide2.pos_frame \
        -x 10 -y 165 -anchor nw -bordermode ignore
    place $slide2.pos_frame.xlabel \
        -x 10 -y 10 -anchor nw -bordermode ignore
    place $slide2.pos_frame.ylabel \
        -x 10 -y 40 -anchor nw -bordermode ignore
    place $slide2.pos_frame.xentry \
        -x 25 -y 10 -width 88 -height 22 -anchor nw -bordermode ignore
    place $slide2.pos_frame.yentry \
        -x 25 -y 40 -width 88 -height 22 -anchor nw -bordermode ignore
    place $slide2.add \
        -x 155 -y 170 -anchor nw -bordermode ignore
    place $slide2.remove \
        -x 210 -y 170 -anchor nw -bordermode ignore
    place $slide2.close \
        -x 180 -y 215 -anchor nw -bordermode ignore

	#----------------------------------------------------------------------#
	# This process will add slidebar2 to the main window          
	#----------------------------------------------------------------------#

   proc add_slidebar2 { } {
	global slide2_name
	global slide2x
	global slide2y
	global slide2_color
	#global scolor
	global v2sl
	global slide2_conn
	global slide2_board
	global slide2_elem_num
	
	set v2sl .ventana2.editor.slide$slide2_name
	
	
       	#----------------------------------------------------------------------#
       	# Get the slide2_board and element number from the connection string passed
	#----------------------------------------------------------------------#
	
       	set slide2_board 2
       	set slide2_elem_num [lindex $slide2_conn 2]

       	#----------------------------------------------------------------------#  	
       	# Check if the user did not forget to insert name or connection
       	#----------------------------------------------------------------------#

      	if {$slide2_name == "" } {
       		puts "Please fill in the Name field "
       		return
       	}


       	if {$slide2_conn == "" } {
       		puts "Please select in the Connection field "
       		return
       	}
       	
       	if {$slide2x == "" } {
       		#puts "Please enter number in the x position entry "
       		set slide2x 0
       		
       	}
       	
       	if {$slide2y == ""  }  {
       		#puts "Please enter number in the y position entry "
       		set slide2y 0
       		
       	}
       	
       	set type [check_out_db $slide2_name $slide2_board $slide2_elem_num]

       	#----------------------------------------------------------------------#
       	# Checking if the button has already been used or mapped before
	#----------------------------------------------------------------------#
 
        set temp_vsl2 .ventana2.editor.slide[get_slidebar_name $slide2_board $slide2_elem_num]
	set temp_vsl_but2 .ventana2.editor.enable($slide2_elem_num)
	set temp_vsl_decr2 .ventana2.editor.incr($slide2_elem_num)
	set temp_vsl_incr2 .ventana2.editor.decr($slide2_elem_num)
		
	             	
 	if {$type > 0 && $type < 9 } {               	
            	# Exists in OUTPUT database                       	
            	set status [tk_dialog  .warning { } {The button has already been used / mapped in Output database. Please Remove it first } warning 0 {Close } ]
       		switch $status {0 {puts "Cancel"; return} }             	
        }


        # Just change the position     	     	
        if { $type == 0 || $type == 10 }  {
       	       	
        	# add it to Slidebar database
            	destroy $temp_vsl2
            	destroy $temp_vsl_but2
		destroy $temp_vsl_incr2
		destroy $temp_vsl_decr2
            		
            	add_to_slidebar_db $slide2_name $slide2_board $slide2_elem_num 0 $slide2x $slide2y  $slide2_color 2

         }

              # Same name but different connection, can't do this.....display an error message
             if {$type == 11} {
             		global env
             		set env(DISPLAY) :0.1
               	set status [tk_dialog  .swarning { } {The Connection has already been used by other slidebar. Please Remove it first } warning 0 {Close } ]
       		        switch $status {0 {puts "Cancel"; return} }             	
              }

            # Same connection different name, user just want to rename it.
            # Destroy old button and then overwrite the name
            if {$type == 12} {	
            		destroy $temp_vsl2
            		destroy $temp_vsl_but2
			destroy $temp_vsl_incr2
			destroy $temp_vsl_decr2
            		
            		add_to_slidebar_db $slide2_name $slide2_board $slide2_elem_num 0 $slide2x $slide2y  $slide2_color 2
	
            }
                # This string show where conn has defined before
             	#set result_string [show_connection_existence $type]
             	#puts "slidebar_popup2.tcl: Slidebar has already exist in $result_string"
             	

             #----------------------------------------------------------------------#
             # This will create the actual slidebar2 widget and 
             # place it in the main screen                     
             #----------------------------------------------------------------------#
             creating_slidebar2



   # end of proc "add_slidebar2"
   }



   proc creating_slidebar2 { } {

       	global slide2_name
	global slide2x
	global slide2y
	global slide2_color
	global v2sl
	global slide2_conn
	global value2
	global color2
	
	
	set v2sl .ventana2.editor.slide$slide2_name
	global $slide2_name
	
        # Setting the default color to grey
 	if {$slide2_color != "grey" && $slide2_color != "red" && $slide2_color != "green" && $slide2_color != "blue" && $slide2_color != "purple" } {
       	 	set slide2_color "grey"       	         	 	
       	}
       	
       	if {$color2 != "" && $color2 != " " && $color2 != 0  &&  $color2 != 1} {
       	    	set slide2_color $color2
       	    	set color2 ""
             }
       	

       	#----------------------------------------------	      	
       	# Check if the user did not forget to insert name or connection
       	#----------------------------------------------

      	if {$slide2_name == "" } {
       		puts "Please fill in the Name field "
       		return
       	}


       	if {$slide2_conn == "" } {
       		puts "Please select in the Connection field "
       		return
       	}
       	          		
        #----------------------------------------------------------------------#
        # Create the slidebar2 here
        # -from is the left / smallest value
   	# -to is the right / highest value
   	# -resolution is the resolution of the slide2 bar, and
   	#   since we want to have decimal number,
   	#   so we set it to be "0.1", the default is "1"
   	#----------------------------------------------------------------------#
        global $slide2_name
        global slide2_board
       	global slide2_elem_num
       	

        scale $v2sl -label $slide2_name -bg $slide2_color  -variable $slide2_name  \
             	-from -100 -to 100  -orient horizontal \
             	-length 150 -resolution 1 -repeatdelay 100000
	
	#----------------------------------------------------------------------#
	# Right now, value is defaulted to 0 everytime we open a file
	# if in the future decided to use the saved value other than 0,
	# then we should make changes to the C++ function 
	# "get_slide_value" 
	#----------------------------------------------------------------------#


	set value2 [get_slide_value $slide2_board $slide2_elem_num]
	set $slide2_name $value2	
             	
	#----------------------------------------------------------------------#
        # Create enable button for the slidebar                 
	#----------------------------------------------------------------------#
	
	global slide2_enable

        global v2sl_but
        set v2sl_but .ventana2.editor.enable($slide2_elem_num)

	button $v2sl_but -text "Enable" \
        	-wraplength 50 -bg $slide2_color -activebackground $slide2_color
        place $v2sl_but -in .ventana2.editor -width 65 -height 59 \
		-x [expr {$slide2x + 157}] -y $slide2y



	#----------------------------------------------------------------------#
	# Create an increment and decrement buttons	
	#----------------------------------------------------------------------#
		
	set v2sl_incr .ventana2.editor.incr($slide2_elem_num)
	button $v2sl_incr -text "+" \
		-bg $slide2_color -activebackground $slide2_color 

	set v2sl_decr .ventana2.editor.decr($slide2_elem_num)
	button $v2sl_decr -text "-" \
		-bg $slide2_color -activebackground $slide2_color 
	
	place $v2sl_incr -in .ventana2.editor -width 50 -height 29 \
		-x [expr {$slide2x - 51}] -y $slide2y
	
	place $v2sl_decr -in .ventana2.editor -width 50 -height 29 \
		-x [expr {$slide2x - 51}] -y [expr {$slide2y + 30}]
	


	#----------------------------------------------------------------------#
	# Bind the Enable button
	#----------------------------------------------------------------------#

		
        bind $v2sl_but <ButtonPress-1> {
	
        	set v2sl_but %W
        	#puts "slidebar_popup.tcl : enable button : $v2sl_but"
             	set temp1 [lindex  [split $v2sl_but ( ]  1]
        	set temp2 [split $temp1 ) ]
		set temp_elem_num [lindex $temp2 0]
        	
		set slide2_name [get_slidebar_name $slide2_board $temp_elem_num ]
        	set v2sl .ventana2.editor.slide$slide2_name
        	
		if { [$v2sl_but cget -relief ] == "raised" } {
		
			#----------------------------------------------------------------------#
			# Enable		
			#----------------------------------------------------------------------#
			
			set slide2_enable($temp_elem_num) 1
			$v2sl_but configure -relief sunken
			$v2sl_but configure -bg white
			$v2sl_but configure -activebackground white
			
			#----------------------------------------------------------------------#
			# Enable the button on the other screen		
			#----------------------------------------------------------------------#
			set vsl_but .ventana.editor.enable($temp_elem_num)        	
			
			if [winfo exists $vsl_but] {
				set slide_enable($temp_elem_num) 1
				$vsl_but configure -relief sunken
				$vsl_but configure -bg white
				$vsl_but configure -activebackground white
			}
			

			#----------------------------------------------------------------------#
			# Add the 0.0 to get decimal number since we want to keep "value"
			# as integer
			#----------------------------------------------------------------------#

			set value [$v2sl get]
			set map_value [expr { ($value + 0.0) / 20 } ]
		
			#-----------------------------------------------#
			# Send the previous set value to bottom		#	
			#-----------------------------------------------#
		
			send_slide_down 2 $temp_elem_num $map_value
 		
		} else {
		
			#----------------------------------------------------------------------#
			# Disable	
			#----------------------------------------------------------------------#
		
			set slide2_enable($temp_elem_num) 0
			$v2sl_but configure -relief raised
			$v2sl_but configure -bg $slide2_color
			$v2sl_but configure -activebackground $slide2_color
			
			#----------------------------------------------------------------------#
			# Disable the button on the other screen		
			#----------------------------------------------------------------------#
			set vsl_but .ventana.editor.enable($temp_elem_num)        	
			
			if [winfo exists $vsl_but] {
				set slide_enable($temp_elem_num) 0
				$vsl_but configure -relief raised
				$vsl_but configure -bg $slide2_color
				$vsl_but configure -activebackground $slide2_color
			}
			
			#----------------------------------------------------------------------#
			# Send 0 to the bottom				
			#----------------------------------------------------------------------#
		
			send_slide_down 2 $temp_elem_num 0
 		
			
		}
	}





	#----------------------------------------------------------------------#
	# Bind the Scale
	#----------------------------------------------------------------------#


	 bind $v2sl <ButtonRelease-1>  {
       		
	 	
	       		 	
       	 	set v2sl %W
       	 	set name [lindex [$v2sl configure -label] 4]
       	 	set sconn [get_slide_connection $name]
       		set connection [split $sconn |]
       		set the_slide2_board [lindex $connection 0]
       		set the_slide2_elem_num [lindex $connection 1]
       		
       		#----------------------------------------------------------------------#
       		# If the slide is disabled, the slidebar value 	
       		# will not be send down and not saved		
       		#----------------------------------------------------------------------#

       		if {$the_slide2_elem_num == "" || $the_slide2_elem_num == -1} { return }       		
     		if {$slide2_enable($the_slide2_elem_num) == 0} { return }
       		

		#----------------------------------------------------------------------#
		# Add the 0.0 to get decimal number since we want to keep "value"
		# as integer
		#----------------------------------------------------------------------#
       		
       	 	set value2 [$v2sl get]
       	 	set map_value2 [expr { ($value2 + 0.0) / 20 } ]           	 	
       	 
       	 	send_slide_down 2 $the_slide2_elem_num $map_value2
       	 	
       	 	

		#----------------------------------------------------------------------#
       	 	# send the value to db     
		#----------------------------------------------------------------------#
       	 	
       	 	set_slide_value 2 $the_slide2_elem_num $value2
  	}
		




	#----------------------------------------------------------------------#
	# Bind the Increment Button
	#----------------------------------------------------------------------#	
	
	
	 bind $v2sl_incr <ButtonPress-1>  {
       		 	          	

        	set v2sl_incr %W
        	set temp1 [lindex  [split $v2sl_incr ( ]  1]
        	set temp2 [split $temp1 ) ]
		set temp_elem_num [lindex $temp2 0]
        	
		
		set slide2_name  [get_slidebar_name $slide2_board $temp_elem_num ]
        	set v2sl .ventana2.editor.slide$slide2_name
        	set v2sl_but .ventana2.editor.enable($temp_elem_num)
        	

		#----------------------------------------------------------------------#
		# Scale is Disabled
		#----------------------------------------------------------------------#
		
		if { [$v2sl_but cget -relief ] == "raised" } {

			incr $slide2_name


		#----------------------------------------------------------------------#
		# Scale is Enabled
		#----------------------------------------------------------------------#
			
		} else {
		
			incr $slide2_name

			set value [$v2sl get]
			set map_value [expr { ($value + 0.0) / 20 } ]
			
			#----------------------------------------------------------------------#
			# Send the value to bottom		
			#----------------------------------------------------------------------#
		
			send_slide_down 2 $temp_elem_num $map_value
 		
			
			
		# end else
		}

	# end binding increment button
	}
	

	#----------------------------------------------------------------------#
	# Bind the Decrement Button
	#----------------------------------------------------------------------#	
	
	
	 bind $v2sl_decr <ButtonPress-1>  {
       		 	          	

        	set v2sl_decr %W
        	set temp1 [lindex  [split $v2sl_decr ( ]  1]
        	set temp2 [split $temp1 ) ]
		set temp_elem_num [lindex $temp2 0]
  
		set slide2_name  [get_slidebar_name $slide2_board $temp_elem_num ]
        	set v2sl .ventana2.editor.slide$slide2_name
        	set v2sl_but .ventana2.editor.enable($temp_elem_num)
        	
		#----------------------------------------------------------------------#
		# Scale is Disabled
		#----------------------------------------------------------------------#
		
		if { [$v2sl_but cget -relief ] == "raised" } {
							
			incr $slide2_name -1


		#----------------------------------------------------------------------#
		# Scale is Enabled
		#----------------------------------------------------------------------#
		
		} else {		
		
			incr $slide2_name -1
		
			set value [$v2sl get]
			set map_value [expr { ($value - 0.0)/ 20 } ]
			
			#-----------------------------------------------#
			# Send the previous set value to bottom		#	
			#-----------------------------------------------#
		
			send_slide_down 2 $temp_elem_num $map_value
 		
			
			
		# end else
		}

	# end binding decrement button
	}
	
	
	#----------------------------------------------------------------------#
	# place the slidebar in the main window            
	#----------------------------------------------------------------------#
	
	place $v2sl -in .ventana2.editor -x $slide2x -y $slide2y
		

   # end of proc "creating_slidebar2"
   }




    #----------------------------------------------------------------------#
    # Procedure to remove the slidebar from screen and database
    #----------------------------------------------------------------------#	

    proc remove_slidebar2 {name screen} {

       global slide2_name
       global slide2_conn

       set conn [get_slide_connection $slide2_name]
       puts "conn here is $conn"
       set connection [split $conn |]
       set elem_num [lindex $connection 1]

       if {$conn != -1} {
       	set slide2_conn "Ana Gesdac $elem_num"
       }





       # Intialize variable
       set rv2sl .ventana2.editor.slide$slide2_name
       set rv2sl_but .ventana2.editor.enable($elem_num)
       set rv2sl_incr .ventana2.editor.incr($elem_num)
       set rv2sl_decr .ventana2.editor.decr($elem_num)


       if [ winfo exists $rv2sl] {
             	destroy $rv2sl
              	destroy $rv2sl_but
              	destroy $rv2sl_incr
              	destroy $rv2sl_decr
              	
             	# Remove slide2 bar from its database
             	remove_slidebar_from_db 2 $elem_num $screen
             	
       }  else {

       	set status [tk_dialog .warning { } {The slidebar you are trying to remove does not exist ! } \
			warning 0 {Close} ]
       	switch $status {
		0 return
       	}

       # end 'else'
       }


    # end of remove_slidebar2 procedure
    }


    proc clean_up2 { } {

    # Declare variables
    global slide2_name
    global slide2x
    global slide2y


    # This statement below will clean up the name and position entry fields from old values

    .slide_popup2.name_entry delete 0 [string length $slide2_name]
    .slide_popup2.slide_connection delete 0 100

    .slide_popup2.pos_frame.xentry delete 0 [string length $slide2x]
    .slide_popup2.pos_frame.yentry delete 0 [string length $slide2y]

    }

# end of proc "slidebar2"
}



proc get_board_number { conn } {

	if {[lindex $conn 1] == "Gesout"} {
		return 0
	}
	if {[lindex $conn 1] == "MPL"} {
		return 1
	}
	if {[lindex $conn 1] == "Gesdac"} {
		return 2
	}


}





