#----------------------------------------------------------------------#
# Include source files
#----------------------------------------------------------------------#
source combo.tcl
source slide_output.tcl


#----------------------------------------------------------------------#
# Initialize variables
#----------------------------------------------------------------------#

for {set i 1 } {$i <= 32 } {incr i } {
	set slide_enable($i) 0
}


proc slidebar_popup { } {

	#----------------------------------------------------------------------#
	# Declare variables
	#----------------------------------------------------------------------#
	global slide_color
	global color
	global slide_enable

	#----------------------------------------------------------------------#
	# Create a new window
	#----------------------------------------------------------------------#
	set slide .slide_popup

	if [winfo exists $slide] {destroy $slide}
	
	global env
	set env(DISPLAY) :0
	toplevel $slide -width 11c -height 9c
	wm title $slide "Creating Slidebar"

	#----------------------------------------------------------------------#
	# Creating Widgets
	#----------------------------------------------------------------------#

	label $slide.slidebar_title \
		-foreground #0000d8 -height 0 -relief groove -text Slidebar -width 299
	label $slide.slide_name_label \
		-height 0 -text Name
	label $slide.conn_label \
		-text Connection
	label $slide.color_label \
		-text Color

	#----------------------------------------------------------------------#
	# Create the listbox for slidebar names
	#----------------------------------------------------------------------#

	slide_name_combobox

	entry $slide.slide_connection -background white -textvariable slide_conn
	button $slide.arrow -text v \
		-command {slide_add_output}

	#----------------------------------------------------------------------#
	# Create the combobox for color
	#----------------------------------------------------------------------#

	slide_color_combobox


	label $slide.pos_label \
		-height 0 -text Position -width 0
	frame $slide.pos_frame \
		-borderwidth 2 -height 75 -relief groove -width 125
	label $slide.pos_frame.xlabel \
		-text x
	label $slide.pos_frame.ylabel \
		-text y
	entry $slide.pos_frame.xentry \
		-background white -textvariable slidex -width 88
	entry $slide.pos_frame.yentry \
		-background white -textvariable slidey -width 88
	button $slide.add \
		-command add_slidebar -height 0 -text Add
	button $slide.remove \
		-command { remove_slidebar $slide_name 1 } -text Remove
	button $slide.close \
		-command {destroy .slide_popup} -text Close



	#----------------------------------------------------------------------#
	# Bind the motion of the mouse to display the mouse
	# coordinate at current time
	#----------------------------------------------------------------------#
	
	bind .ventana <ButtonRelease-1> {
		global slidex slidey
		set slidex %x
		set slidey %y
	}	



	#----------------------------------------------------------------------#
	# SETTING GEOMETRY
	#----------------------------------------------------------------------#
	
	
	#----------------------------------------------------------------------#
	# Connection entry and the arrow button beside it
	#----------------------------------------------------------------------#
	
	place $slide.slide_connection \
		-x 95 -y 75 -anchor nw -bordermode ignore
	place $slide.arrow \
		-x 243 -y 75 -width 26 -height 21 -anchor nw -bordermode ignore

	place $slide.slidebar_title \
		-x 10 -y 10 -width 299 -height 20 -anchor nw -bordermode ignore
	place $slide.slide_name_label \
		-x 10 -y 45 -anchor nw -bordermode ignore
	place $slide.conn_label \
		-x 10 -y 75 -anchor nw -bordermode ignore
	place $slide.color_label \
		-x 10 -y 105 -anchor nw -bordermode ignore
	place $slide.pos_label \
		-x 10 -y 140 -anchor nw -bordermode ignore
	place $slide.pos_frame \
		-x 10 -y 165 -anchor nw -bordermode ignore
	place $slide.pos_frame.xlabel \
		-x 10 -y 10 -anchor nw -bordermode ignore
	place $slide.pos_frame.ylabel \
		-x 10 -y 40 -anchor nw -bordermode ignore
	place $slide.pos_frame.xentry \
		-x 25 -y 10 -width 88 -height 22 -anchor nw -bordermode ignore
	place $slide.pos_frame.yentry \
		-x 25 -y 40 -width 88 -height 22 -anchor nw -bordermode ignore
	place $slide.add \
		-x 155 -y 170 -anchor nw -bordermode ignore
	place $slide.remove \
		-x 210 -y 170 -anchor nw -bordermode ignore
	place $slide.close \
		-x 180 -y 215 -anchor nw -bordermode ignore

	#----------------------------------------------------------------------#
	# This process will add slidebar to the main window          
	#----------------------------------------------------------------------#

	proc add_slidebar { } {
	
		global slide_name slidex slidey slide_color vsl slide_conn slide_board slide_elem_num
	
		set vsl .ventana.editor.slide$slide_name
	     	
		#----------------------------------------------------------------------#
       		# Check if the user did not forget to insert name or connection
       		#----------------------------------------------------------------------#

		if {$slide_name == "" } {
			puts "Please fill in the Name field "
			return
		}


		if {$slide_conn == "" } {
			puts "Please select in the Connection field "
			return
		}
       	
		if {$slidex == "" } {
			#puts "Please enter number in the x position entry "
			set slidex 0
       		
		}
       	
		if {$slidey == "" } {
			#puts "Please enter number in the y position entry "
			set slidey 0     		
		}
       	
		#------------------------------------------------------------------------------#
		# Get the slide_board and element number from the connection string passed
		#-----------------------------------------------------------------------------#
		set slide_board 2
		set slide_elem_num [lindex $slide_conn 2]

		set type [check_out_db $slide_name $slide_board $slide_elem_num]

		#-----------------------------------------------------------------------------#
		# Checking if the button has already been used or mapped before
		#-----------------------------------------------------------------------------#
        	
		set temp_vsl .ventana.editor.slide[get_slidebar_name $slide_board $slide_elem_num]
		set temp_vsl_but .ventana.editor.enable($slide_elem_num)
		set temp_vsl_incr .ventana.editor.incr($slide_elem_num)
		set temp_vsl_decr .ventana.editor.decr($slide_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_vsl
            		destroy $temp_vsl_but
            		destroy $temp_vsl_incr
            		destroy $temp_vsl_decr

            		
            		add_to_slidebar_db $slide_name $slide_board $slide_elem_num 0 $slidex $slidey  $slide_color  1
            }

            # Same name but different connection, can't do this.....display an error message
             if {$type == 11} {
             		global env
             		set env(DISPLAY) :0
               	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_vsl
            	destroy $temp_vsl_but
            	destroy $temp_vsl_incr
           	destroy $temp_vsl_decr
            	
            	add_to_slidebar_db $slide_name $slide_board $slide_elem_num 0 $slidex $slidey  $slide_color  1
        		
            }



             #----------------------------------------------------------------------#
             # This will create the actual slidebar widget and 
             # place it in the main screen                     
             #----------------------------------------------------------------------#
             creating_slidebar



   # end of proc "add_slidebar"
   }



   proc creating_slidebar { } {

       	global slide_name
	global slidex
	global slidey
	global slide_color
	global vsl
	global slide_conn
	global value
	global color
	
	
	set vsl .ventana.editor.slide$slide_name
	global $slide_name
	
        # Setting the default color to grey
 	if {$slide_color != "grey" && $slide_color != "red" && $slide_color != "green" && $slide_color != "blue" && $slide_color != "purple" } {
       	 	set slide_color "grey"       	         	 	
       	}
       	
       	if {$color != "" && $color != " " && $color != 0  &&  $color != 1}  {
       	    	set slide_color $color
       	    	set color ""
             }
       	

       	#----------------------------------------------------------------------#  	
       	# Check if the user did not forget to insert name or connection
       	#----------------------------------------------------------------------#

      	if {$slide_name == "" } {
       		puts "Please fill in the Name field "
       		return
       	}


       	if {$slide_conn == "" } {
       		puts "Please select in the Connection field "
       		return
       	}
       	          		
        #----------------------------------------------------------------------#
        # Create the slidebar here
        # -from is the left / smallest value
   	# -to is the right / highest value
   	# -resolution is the resolution of the slide bar, and
   	#   since we want to have decimal number,
   	#   so we set it to be "0.1", the default is "1"
    	#----------------------------------------------------------------------#
        global $slide_name
        global slide_board
       	global slide_elem_num
       	

        scale $vsl -label $slide_name -bg $slide_color  -variable $slide_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 value [get_slide_value $slide_board $slide_elem_num]
	set $slide_name $value	

        #----------------------------------------------------------------------#
        # Create enable button for the slidebar                  
        #----------------------------------------------------------------------#
        global slide_enable
        set slide_enable(slide_elem_num) 0

        global vsl_but
        set vsl_but .ventana.editor.enable($slide_elem_num)

	button $vsl_but -text "Enable" \
        	-wraplength 50 -bg $slide_color -activebackground $slide_color
	place $vsl_but -in .ventana.editor -width 65 -height 59 \
		-x [expr {$slidex + 157}] -y $slidey
		

	#----------------------------------------------------------------------#
	# Create an increment and decrement buttons	
	#----------------------------------------------------------------------#
		
	set vsl_incr .ventana.editor.incr($slide_elem_num)
	button $vsl_incr -text "+" \
		-bg $slide_color -activebackground $slide_color 

	set vsl_decr .ventana.editor.decr($slide_elem_num)
	button $vsl_decr -text "-" \
		-bg $slide_color -activebackground $slide_color 
	
	place $vsl_incr -in .ventana.editor -width 50 -height 29 \
		-x [expr {$slidex - 51}] -y $slidey
	
	place $vsl_decr -in .ventana.editor -width 50 -height 29 \
		-x [expr {$slidex - 51}] -y [expr {$slidey + 30}]
	


	#----------------------------------------------------------------------#
	# Bind the Enable button
	#----------------------------------------------------------------------#

        bind $vsl_but <ButtonPress-1> {
	
        	set vsl_but %W
        	#puts "slidebar_popup.tcl : enable button : $vsl_but"
             	set temp1 [lindex  [split $vsl_but ( ]  1]
        	set temp2 [split $temp1 ) ]
		set temp_elem_num [lindex $temp2 0]
        			
		set slide_name  [get_slidebar_name $slide_board $temp_elem_num ]
        	set vsl .ventana.editor.slide$slide_name
        	#puts "slidebar_popup.tcl: vsl_but here is : $vsl_but"
        	
		if { [$vsl_but cget -relief ] == "raised" } {
		
			#----------------------------------------------------------------------#
			# Enable	
			#----------------------------------------------------------------------#
		
			set slide_enable($temp_elem_num) 1
			$vsl_but configure -relief sunken
			$vsl_but configure -bg white
			$vsl_but configure -activebackground white
			
			#----------------------------------------------------------------------#
			# Enable the button on the other screen		
			#----------------------------------------------------------------------#
			set v2sl_but .ventana2.editor.enable($temp_elem_num)        	
			
			if [winfo exists $v2sl_but] {
				set slide2_enable($temp_elem_num) 1
				$v2sl_but configure -relief sunken
				$v2sl_but configure -bg white
				$v2sl_but configure -activebackground white
			}
	
			#----------------------------------------------------------------------#
			# Add the 0.0 to get decimal number since we want to keep "value"
			# as integer
			#----------------------------------------------------------------------#

			set value [$vsl 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 slide_enable($temp_elem_num) 0
			$vsl_but configure -relief raised
			$vsl_but configure -bg $slide_color
			$vsl_but configure -activebackground $slide_color
			
			#----------------------------------------------------------------------#
			# Enable the button on the other screen		
			#----------------------------------------------------------------------#
			set v2sl_but .ventana2.editor.enable($temp_elem_num)        	
			
			if [winfo exists $v2sl_but] {
				set slide2_enable($temp_elem_num) 0
				$v2sl_but configure -relief raised
				$v2sl_but configure -bg $slide_color
				$v2sl_but configure -activebackground $slide_color
			}
						
			#----------------------------------------------------------------------#
			# Send 0 to the bottom					
			#----------------------------------------------------------------------#
		
			send_slide_down 2 $temp_elem_num 0
 		
		# end else		
		}

	# end binding enable button
	}


	#----------------------------------------------------------------------#
	# Bind the Scale
	#----------------------------------------------------------------------#	
	
	
	 bind $vsl <ButtonRelease-1>  {
       		 	
	 	catch [tkScaleIncrement]     	
          	
       	 	set vsl %W
       	 	set name [lindex [$vsl configure -label] 4]
       	 	set sconn [get_slide_connection $name]
       		set connection [split $sconn |]
       		set the_slide_board [lindex $connection 0]
       		set the_slide_elem_num [lindex $connection 1]
       		
       	 	#----------------------------------------------------------------------#
       		# If the slide is disabled, the slidebar value 	
       		# will not be send down and not saved		
       		#----------------------------------------------------------------------#

       		if {$the_slide_elem_num == "" || $the_slide_elem_num == -1} { return }       		
       		if {$slide_enable($the_slide_elem_num) == 0} { return }
       		
   
		#----------------------------------------------------------------------#
		# Add the 0.0 to get decimal number since we want to keep "value"
		# as integer
		#----------------------------------------------------------------------#

    	 	set value [$vsl get]		
       	 	set map_value [expr { ($value + 0.0) / 20 } ]

       	 	#puts "2 $the_slide_elem_num $map_value"
       	 	send_slide_down 2 $the_slide_elem_num $map_value
 		
       		
       	 	#------------------------#      	 	
       	 	# Send the value to db   #
       	 	#------------------------#
       	 	set_slide_value 2 $the_slide_elem_num $value
  	}





	#----------------------------------------------------------------------#
	# Bind the Increment Button
	#----------------------------------------------------------------------#	
	
	
	 bind $vsl_incr <Button-1>  {
       		 	          	

        	set vsl_incr %W
        	set temp1 [lindex  [split $vsl_incr ( ]  1]
        	set temp2 [split $temp1 ) ]
		set temp_elem_num [lindex $temp2 0]
		
		set slide_name  [get_slidebar_name $slide_board $temp_elem_num ]
        	set vsl .ventana.editor.slide$slide_name
        	set vsl_but .ventana.editor.enable($temp_elem_num)
        		
		#----------------------------------------------------------------------#
		# Scale is Disabled
		#----------------------------------------------------------------------#
				
		if { [$vsl_but cget -relief ] == "raised" } {
					
			incr $slide_name


		#----------------------------------------------------------------------#
		# Scale is Enabled
		#----------------------------------------------------------------------#
		
		} else {
		
			incr $slide_name

			set value [$vsl get]			
			set map_value [expr { ($value + 0.0) / 20 } ]
			
			#----------------------------------------------------------------------#
			# Send the value to bottom			
			#----------------------------------------------------------------------#
			
			send_slide_down 2 $temp_elem_num $map_value
 		
			
		# end if
		}

	# end binding increment button
	}
	

	#----------------------------------------------------------------------#
	# Bind the Decrement Button
	#----------------------------------------------------------------------#	
	
	
	 bind $vsl_decr <ButtonPress-1>  {
       		 	          	

        	set vsl_decr %W
        	set temp1 [lindex  [split $vsl_decr ( ]  1]
        	set temp2 [split $temp1 ) ]
		set temp_elem_num [lindex $temp2 0]
        	
		
		set slide_name  [get_slidebar_name $slide_board $temp_elem_num ]
        	set vsl .ventana.editor.slide$slide_name
        	set vsl_but .ventana.editor.enable($temp_elem_num)
        	
		
		#----------------------------------------------------------------------#
		# Scale is Disabled
		#----------------------------------------------------------------------#
		
		if { [$vsl_but cget -relief ] == "raised" } {
								
			incr $slide_name -1			
			
		#----------------------------------------------------------------------#
		# Scale is Enabled
		#----------------------------------------------------------------------#
			
		} else {
		
			incr $slide_name -1			
			
			set value [$vsl 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 decrement button
	}
	









  	#----------------------------------------------------------------------#
	# place the slidebar in the main window 	
	#----------------------------------------------------------------------#	
             	
	place $vsl -in .ventana.editor -x $slidex -y $slidey
		
	



   # end of proc "creating_slidebar"
   }





    #----------------------------------------------------------------------#
    # Procedure to remove the slidebar from screen and database
    #----------------------------------------------------------------------#	
	

    proc remove_slidebar {name screen} {

       global slide_name
       global slide_conn

       set conn [get_slide_connection $slide_name]
       puts "conn here is $conn"
       set connection [split $conn |]
       set elem_num [lindex $connection 1]

       if {$conn != -1} {
       	set slide_conn "Ana Gesdac $elem_num"
       }

       # Intialize variable
       set rvsl .ventana.editor.slide$slide_name
       set rvsl_but .ventana.editor.enable($elem_num)
	set rvsl_incr .ventana.editor.incr($elem_num)
	set rvsl_decr .ventana.editor.decr($elem_num)

       if [ winfo exists $rvsl] {
             	destroy $rvsl
             	destroy $rvsl_but
            	destroy $rvsl_incr
           	destroy $rvsl_decr
            	

             	# Remove slide bar from its database (board, elem_num, screen)
             	remove_slidebar_from_db 2 $elem_num 1
             	
       }  else {

              global env
              set env(DISPLAY) :0
       	set status [tk_dialog .swarning { } {The slidebar you are trying to remove does not exist ! } \
			warning 0 {Close} ]
       	switch $status {
		0 return
       	}

       # end 'else'
       }


    # end of remove_slidebar procedure
    }


# end of proc "slidebar_popup"
}


    proc clean_up { } {

    # Declare variables
    global slide_name
    global slidex
    global slidey


    # This statement below will clean up the name and position entry fields from old values

    .slide_popup.name_entry delete 0 [string length $slide_name]
    .slide_popup.slide_connection delete 0 100

    .slide_popup.pos_frame.xentry delete 0 [string length $slidex]
    .slide_popup.pos_frame.yentry delete 0 [string length $slidey]

    }


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
	}


}

