##################################################################
# COMBOBOX
##################################################################


#-----------------------------------------------------------------------------------
# Declare these variables as global so that we can use that in other procedure
# inside 'elem_prop.tcl' file where we have to send these values to c++ database
#-----------------------------------------------------------------------------------
global graph_type
global default_type



#---------------------------------
#  Initialize the variables
#---------------------------------
set graph_surveh 0
set graph_diganal 0
set graph_elem_num 0


#--------------------------------------------------------------------------------#
# This procedure create a combobox that has a list of graphical items            #
# can be displayed on main menu                                                  #
#--------------------------------------------------------------------------------#

proc graphic_type_combobox {sv da en } {

   # Declare variables since they are going to be used inside functions
   global ve
   global gbut_click
   global graph_surveh
   global graph_diganal
   global graph_elem_num


   # Setting variables
   set ve .bpm.graph_info
   set graph_surveh $sv
   set graph_diganal $da
   set graph_elem_num $en


   frame $ve.gef
   entry $ve.gef.graphic_entry -bg white -textvariable graph_type -width 13
   button $ve.gef.arrow -command {show_glist }

   # Clean up the entry widget from previous value
   #$ve.gef.graphic_entry delete 0 100

   # pack the entry and arrow button together on top
   place $ve.gef -x 80 -y 20 -width 135 -height 22 -anchor nw -bordermode ignore
   pack $ve.gef.graphic_entry $ve.gef.arrow -side left

   # initially, the button is only clicked once
   set gbut_click 1

   #----------------------------------------------------------------------#
   # this procedure is to show the listbox and hide it when each time the #
   # arrow button clicked                                                 #
   #----------------------------------------------------------------------#

   proc show_glist { } {
      global ve
      global gbut_click
         if {$gbut_click == 2} {
            if [winfo exists $ve.gel] {
       		destroy $ve.gel
            }

         set gbut_click 1

      } else {
         create_glist

         pack $ve.gel.graphic_list $ve.gel.scroll -side left -fill y -expand 1
         place $ve.gel -x 80 -y 44  -anchor nw -bordermode ignore
         set gbut_click 2
      }

      # end of procedure "show_glist"
   }


   #------------------------------------------------------------------#
   # This procedure will actually create the listbox and its data     #
   #------------------------------------------------------------------#

   proc create_glist { } {
      global ve
      global graph_surveh
      global graph_diganal

      # create the listbox and scrollbar widget
      frame $ve.gel
      scrollbar $ve.gel.scroll -command {$ve.gel.graphic_list yview}
      listbox $ve.gel.graphic_list -width 13 -height 3 -yscroll "$ve.gel.scroll set"

      #----------------------------------#
      # if it is an sub analog element   #
      #----------------------------------#

      if {$graph_surveh == 0 && $graph_diganal == 0 } {
         # inserting data into the listbox
         $ve.gel.graphic_list insert end "None"
         $ve.gel.graphic_list insert end "Bar"
         $ve.gel.graphic_list insert end "Depth 1"
         $ve.gel.graphic_list insert end "Depth 2"
         $ve.gel.graphic_list insert end "Depth 3"

      } else {
      	# if it is digital element
         $ve.gel.graphic_list insert end "None"
      }



      # Select the graphic type using the mouse click
      bind $ve.gel.graphic_list <ButtonRelease-1> {
         global graph_type
         global gr
         set len [string length $graph_type ]
         set graph_type [selection get]

      # end of binding
      }

   # end of procedure "create_glist"
   }

# end of procedure "graphic_type_combobox"
}






##################################################################
# BUTTON
##################################################################

#----------------------------------------------------
# This procedure is called to create combobox to select  the
# color for button pressed
# It is called inside " but_small_popup.tcl "
#----------------------------------------------------


proc color_combobox { } {

   # Declare variable
   global clrbut_click

   #puts "combo.tcl: but color in here : $but_color "

   # Create frame that contains the entry and the arrow button beside the entry slot
   frame .but_small_popup.clrcombo
   entry .but_small_popup.clrcombo.color_entry -bg white -textvariable but_color -width 20
   button .but_small_popup.clrcombo.arrow -command {show_clrlist }

   # clean up the entry widget from previous value
   .but_small_popup.clrcombo.color_entry delete 0 100

   # pack the entry and arrow button together on top
   place .but_small_popup.clrcombo -x 104 -y 110 -width 200 -height 22 \
	-anchor nw -bordermode ignore
   pack .but_small_popup.clrcombo.color_entry .but_small_popup.clrcombo.arrow -side left

   # initially, the button is only clicked once
   set clrbut_click 1

   # this procedure is to show the listbox and hide it when each time the
   # arrow button clicked
   proc show_clrlist { } {
      global clrbut_click

      if {$clrbut_click == 2} {

         if [winfo exists .but_small_popup.color] {
            destroy .but_small_popup.color
         }

         set clrbut_click 1

      } else {

         create_clrlist
         set clrbut_click 2
      }

   # end of proc "show_clrlist"
   }

   # This procedure will actually create the listbox and its data
      proc create_clrlist { } {

         # create the listbox and scrollbar widget
         frame .but_small_popup.color
         scrollbar .but_small_popup.color.scroll -command {.but_small_popup.color.color_list yview}
         listbox .but_small_popup.color.color_list -width 20 -height 4 -yscroll ".but_small_popup.color.scroll set"

         pack .but_small_popup.color.color_list .but_small_popup.color.scroll -side left -fill y -expand 1
         place .but_small_popup.color -x 104 -y 130  -anchor nw -bordermode ignore


            # inserting data into the listbox
            .but_small_popup.color.color_list insert end "grey"
            .but_small_popup.color.color_list insert end "red"
            .but_small_popup.color.color_list insert end "green"
            .but_small_popup.color.color_list insert end "blue"
            .but_small_popup.color.color_list insert end "purple"


         # Select the button color using mouse click
         bind .but_small_popup.color.color_list <ButtonRelease-1> {
            global but_color

            # Display the selection in the entry widget
            set but_color [selection get]

         # end of binding
         }

      # end of procedure "create_clrlist"
      }

   # end of procedure "color_combobox"
   }


##################################################################
# SLIDEBAR
##################################################################

#----------------------------------------------------
# This procedure is called to create combobox to select the
# color for button pressed
# It is called inside "slidebar_popup.tcl "
#----------------------------------------------------


proc slide_color_combobox { } {

   # Declare variable
   global slide_clrbut_click

   # Create frame that contains the entry and the arrow button beside the entry slot
   frame .slide_popup.clrcombo
   entry .slide_popup.clrcombo.color_entry -bg white -textvariable slide_color -width 20
   button .slide_popup.clrcombo.arrow -command {show_slide_clrlist }

   # clean up the entry widget from previous value
   .slide_popup.clrcombo.color_entry delete 0 100

   # pack the entry and arrow button together on top
   place .slide_popup.clrcombo -x 95 -y 105 -width 200 -height 22 \
	-anchor nw -bordermode ignore
   pack .slide_popup.clrcombo.color_entry .slide_popup.clrcombo.arrow -side left

   # initially, the button is only clicked once
   set slide_clrbut_click 1

   # this procedure is to show the listbox and hide it when each time the
   # arrow button clicked
   proc show_slide_clrlist { } {
      global slide_clrbut_click

      if {$slide_clrbut_click == 2} {

         if [winfo exists .slide_popup.color] {
            destroy .slide_popup.color
         }

         set slide_clrbut_click 1

      } else {


         create_slide_clrlist
         pack .slide_popup.color.color_list .slide_popup.color.scroll -side left -fill y -expand 1
         place .slide_popup.color -x 95 -y 125  -anchor nw -bordermode ignore
         set slide_clrbut_click 2
      }

   # end of proc "show_slide_clrlist"
   }

   # This procedure will actually create the listbox and its data
      proc create_slide_clrlist { } {

         # create the listbox and scrollbar widget
         frame .slide_popup.color
         scrollbar .slide_popup.color.scroll -command {.slide_popup.color.color_list yview}
         listbox .slide_popup.color.color_list -width 20 -height 4 -yscroll ".slide_popup.color.scroll set"

            # inserting data into the listbox
            .slide_popup.color.color_list insert end "grey"
            .slide_popup.color.color_list insert end "red"
            .slide_popup.color.color_list insert end "green"
            .slide_popup.color.color_list insert end "blue"
            .slide_popup.color.color_list insert end "purple"

         # Select the button color using mouse click
         bind .slide_popup.color.color_list <ButtonRelease-1> {
            global slide_color

            # Display the selection in the entry widget
            set slide_color [selection get]

         # end of binding
         }

      # end of procedure "create_slide_clrlist"
      }

   # end of procedure "slide_color_combobox"
   }



#-----------------------------------------------------------------------#
# This combobox contains all the names of the button names that are	#
# exist in the database              					#
#-----------------------------------------------------------------------#


proc button_name_combobox { } {

   	# Declare variables since they are going to be used inside functions
   	global bname_but_click

   	# frame to hold the entry and arrow button
   	frame .but_small_popup.combo
   	entry .but_small_popup.combo.name_entry -bg white -textvariable but_name -width 20
   	button .but_small_popup.combo.arrow -command {show_bname_list }

        place .but_small_popup.combo -x 105 -y 50 -width 200 -height 22 -anchor nw -bordermode ignore
   	pack .but_small_popup.combo.name_entry .but_small_popup.combo.arrow -side left

   	set bname_but_click 1
   	
   	   	
   	#-------------------------------------------------------#
   	# this procedure is to show the listbox and hide       	#
   	# it when each time the arrow button clicked		#
   	#-------------------------------------------------------#
   	
   	proc show_bname_list { } {
      		global bname_but_click

      		if {$bname_but_click == 2} {
        		if [winfo exists .but_small_popup.dlf] {
          			destroy .but_small_popup.dlf
      			}

      			set bname_but_click 1
      			
   	        } else {
   	
   	        	create_bname_list

         		pack .but_small_popup.dlf.bname_list .but_small_popup.dlf.scroll -side left -fill y -expand 1
         		place .but_small_popup.dlf -x 105 -y 75  -anchor nw -bordermode ignore
         		set bname_but_click 2
      		}
      	
      	# end procedure "show_bname_list"   	  	
   	}
   	
   	#----------------------------------------#
   	# This procedure will actually create    #
   	# the listbox and its data               #
   	#----------------------------------------#
   	proc create_bname_list { } {

   		#-------------------------------------------#
      		# create the listbox and scrollbar widget   #
      		#-------------------------------------------#
      		
      		frame .but_small_popup.dlf
      		scrollbar .but_small_popup.dlf.scroll -command {.but_small_popup.dlf.bname_list yview}
      		listbox .but_small_popup.dlf.bname_list -width 20 -height 4 -yscroll ".but_small_popup.dlf.scroll set"

      		#---------------------------------------#      		
                # inserting data into the listbox	#	
                #---------------------------------------#
      		
                set button_size [get_bcount]

                for {set i 0} {$i < $button_size} {incr i} {
                      	.but_small_popup.dlf.bname_list insert end [get_bname $i]
         	}
         	
         	#-------------------------------------------#
         	# Select the name using the mouse click  #
         	#-------------------------------------------#
         	
      		bind .but_small_popup.dlf.bname_list <ButtonRelease-1> {
         		global but_name conn_name sp_xentry sp_yentry
         		global but_default1 but_default2 but_default3 but_default4
         		
         		# Button Name
         		set but_name [selection get]

         		# Button Connection
         		set board -1
         		set elem_num -1
         		set belnum -1
         		
         		set temp [split [get_button_connection $but_name] | ]
         		set board [lindex $temp 0]
         		set elem_num [lindex $temp 1]
         		
         		#puts "but_name : $but_name"
         		#puts "board : $board"
         		#puts "elem_num : $elem_num"
         		
         		
         		if {$board == 0} {
         			set the_conn "Dig Gesout $elem_num"
         			# This is to find the IO element number for this button
         			set belnum [expr {$elem_num + 100}]
         		}
         		
         		if {$board == 1} {         			
         			set the_conn "Dig MPL $elem_num"
         			# This is to find the IO element number for this button
         			set belnum [expr {$elem_num + 200}]
         		}
         		
         		set conn_name $the_conn

			#------------------------------------------------------------------#
			# Button color and type 
			#------------------------------------------------------------------#
						
			global varname but_color
			set but_color [lindex [get_button_color_type $board $elem_num] 0]
			set varname [lindex [get_button_color_type $board $elem_num] 1]
			
			#puts "but_small_popup.tcl : varname : $varname"

   			#------------------------------------------------------------------#
			# Button Position on Screen 1
			#------------------------------------------------------------------#
			
         		set pos [get_button_pos $board $elem_num 1]
      			set sp_xentry [lindex $pos 0]
      			set sp_yentry [lindex $pos 1]

                        # Display Default For Selected Button
                        set bdef [lindex [split [ret_all_info 1 1 $belnum 1] |] 4]
         		set but_default1 [lindex [split $bdef :] 0]
         		set but_default2 [lindex [split $bdef :] 1]
         		set but_default3 [lindex [split $bdef :] 2]
         		set but_default4 [lindex [split $bdef :] 3]
         		puts "combo.tcl : all = [split [ret_all_info 1 1 $belnum 1] |]"
         		puts "combo.tcl : bdef = $bdef"
         		puts "combo.tcl : belnum = $belnum"
         		
      		# end of binding
      		}

   	# end of procedure "create_bname_list"
   	}
   	

# end of procedure "button_name_combobox"
}
	


#-----------------------------------------------------------------------#
# This combobox contains all the names of the slidebar names that are	#
# exist in the database              					#
#-----------------------------------------------------------------------#

proc slide_name_combobox { } {

   	# Declare variables since they are going to be used inside functions
   	global sname_but_click

   	# frame to hold the entry and arrow button
   	frame .slide_popup.combo
   	entry .slide_popup.combo.name_entry -bg white -textvariable slide_name -width 20
   	button .slide_popup.combo.arrow -command {show_sname_list }

        place .slide_popup.combo -x 95 -y 45 -width 200 -height 22 -anchor nw -bordermode ignore
   	pack .slide_popup.combo.name_entry .slide_popup.combo.arrow -side left

   	set sname_but_click 1
   	
   	# this procedure is to show the listbox and hide it
   	# when each time the arrow button clicked
   	proc show_sname_list { } {
      		global sname_but_click

      		if {$sname_but_click == 2} {
        		if [winfo exists .slide_popup.dlf] {
          			destroy .slide_popup.dlf
      			}

      			set sname_but_click 1
      			
   	        } else {
   	
   	        	create_sname_list

         		pack .slide_popup.dlf.bname_list .slide_popup.dlf.scroll -side left -fill y -expand 1
         		place .slide_popup.dlf -x 95 -y 65  -anchor nw -bordermode ignore
         		set sname_but_click 2
      		}
      	
      	# end procedure "show_sname_list"
   	  	
   	}
   	
   	# This procedure will actually create the listbox and its data
   	proc create_sname_list { } {

      		# create the listbox and scrollbar widget
      		frame .slide_popup.dlf
      		scrollbar .slide_popup.dlf.scroll -command {.slide_popup.dlf.bname_list yview}
      		listbox .slide_popup.dlf.bname_list -width 20 -height 4 -yscroll ".slide_popup.dlf.scroll set"

                # inserting data into the listbox
                set slidebar_size [get_scount]

                for {set i 0} {$i < $slidebar_size} {incr i} {
                      	.slide_popup.dlf.bname_list insert end [get_sname $i]
         	}
         	
         	# Select the name using the mouse click
      		bind .slide_popup.dlf.bname_list <ButtonRelease-1> {
         		global slide_name slide_conn slidex slidey
         		
         		# Slidebar Name
         		set slide_name [selection get]
         		
         		# Slidebar Connection
         		set temp [split [get_slide_connection $slide_name] | ]
         		set elem_num [lindex $temp 1]
         		set the_conn "Ana Gesdac $elem_num"
         		set slide_conn $the_conn

         		# Slidebar Position on screen 1
         		set pos [get_slidebar_pos $elem_num 1]
         		set slidex [lindex $pos 0]
         		set slidey [lindex $pos 1]
         		

      		# end of binding
      		}

   	# end of procedure "create_sname_list"
   	}
   	

# end of procedure "slide_name_combobox"
}
	



