# Bar Graphics use the element Number to display first the bar, then the line and value
# The line and value are destroyed and created new every second
# the alarm function has been disabled for now
# If there are more than 48 analog up traffic channels then the Max i count has to be increased below

for { set i 0 } { $i <= 64 } { incr i } {
	set barx($i) 0
	set bary($i) 0	
	set bar_check_var($i) 1
	set vbf$i ""
	set lbar($i) 0
	set vtext_id($i) 0
	set bar_timer($i) 0
	set bar_min($i) 0
	set bar_max($i) 0
	set bar_average($i) 0
	set bar_yrange($i) 0
	set bar_rrange($i) 0
}

#--------------------------------------------------------------------
# Make the Changes on the size of the bargraph here
# bar_x_left_coord is the left x coordinate
# bar_x_right_coord is the right x coordinate
# bar_y_top_coord is the top y coordinate
# bar_y_bottom_coord is the bottom y coordinate
#--------------------------------------------------------------------

set bar_x_left_coord 30
set bar_y_top_coord 40
set bar_x_right_coord 40
set bar_y_bottom_coord 140

#--------------------------------------------------------------------


# "elem_num" is the element number for vehicle analog in
proc display_bar_or_not { flag elem_num } {
    	    	
   	global bar_check_var
   	global barx bary
	global bar_x_right_coord bar_y_bottom_coord

        global env
	set env(DISPLAY) :0
	
	set vbf .ventana.editor.bframe$elem_num
	set vbar $vbf.barc
	
	if {$flag == 1 && $elem_num >= 1} {
   		if [winfo exists $vbf] { destroy $vbf }
		set bar_check_var($elem_num) 1
		
		frame $vbf -background black -width [expr {$bar_x_right_coord + 15}] -height [expr {$bar_y_bottom_coord + 25 }]
		place $vbf -x $barx($elem_num) -y $bary($elem_num)
		
		# Create the canvas to draw the bar graph
   		canvas $vbar -width 100 -height 250 -background black
   		place $vbar -x -1 -y -1

		# Create the bar graph here
		creating_graphic $vbar $elem_num
  	} else {
  		set bar_check_var($elem_num) 0
  		destroy $vbf   	
  	}
}


#--------------------------------------------------------------------


proc creating_graphic {vbar elem_num} {

   global bar_min bar_max bar_average bar_yrange bar_rrange
   global bar_x_left_coord bar_y_top_coord bar_x_right_coord bar_y_bottom_coord
   global lbar

   #-----------------------------------------
   # Create the outer rectangle x1 y1 x2 y2
   # remember that (0,0) starts at top left corner and
   # x going to right , y going down as they increases
   #-----------------------------------------

   set outer [ $vbar create rectangle $bar_x_left_coord $bar_y_top_coord $bar_x_right_coord $bar_y_bottom_coord -outline white ]

   # Map the range according to the canvas coordinate	
   # Ratio is in Pixels per units (Volts, PSI, etc.)

   if { [expr { ($bar_min($elem_num) - $bar_max($elem_num)) } ] } {
	set ratio [ expr { double(($bar_y_bottom_coord - $bar_y_top_coord)) / ($bar_min($elem_num) - $bar_max($elem_num)) } ]
   } else {
   	set ratio 0
   	}
   # Bar Average is in pixels from top of graph
   set temp_bar_average [ expr {$bar_y_bottom_coord - (($bar_average($elem_num) - $bar_max($elem_num)) * $ratio) } ]

   # Convert the units to pixels then add subtract from bar average

   set bar_low_yrange [ expr { $temp_bar_average + $bar_yrange($elem_num) * $ratio } ]
   set bar_up_yrange  [ expr { $temp_bar_average - $bar_yrange($elem_num) * $ratio } ]
   set bar_low_rrange [ expr { $temp_bar_average + $bar_rrange($elem_num) * $ratio } ]
   set bar_up_rrange  [ expr { $temp_bar_average - $bar_rrange($elem_num) * $ratio } ]

   #----------------------------------
   # Drawing the three color rectangles
   #----------------------------------

   # Draw the red rectangle
   set red_rect [ $vbar create rectangle $bar_x_left_coord $bar_y_top_coord $bar_x_right_coord $bar_y_bottom_coord \
		-fill red -outline white ]
   # Draw the yellow rectangle	
   set yellow_rect [ $vbar create rectangle $bar_x_left_coord $bar_up_rrange $bar_x_right_coord $bar_low_rrange \
		-fill yellow -outline white]

   # Draw the green rectangle
   set green_rect [ $vbar create rectangle $bar_x_left_coord $bar_up_yrange $bar_x_right_coord $bar_low_yrange \
		-fill green -outline white]
   	

   #--------------------------------------------
   # Creating the line and its scales beside the bar graph
   #--------------------------------------------

   # Create the main line
   $vbar create line $bar_x_left_coord $bar_y_top_coord $bar_x_left_coord $bar_y_bottom_coord -fill white

   # Create the little mark/scale on the main line every 5 pixels
   for {set i $bar_y_top_coord} {$i <= $bar_y_bottom_coord} {incr i 5} {
        $vbar create line [expr {$bar_x_left_coord - 2}] $i $bar_x_left_coord $i -fill white
   }

   # Create a longer mark/scale on the main line every 25 pixels
   for {set i $bar_y_top_coord} {$i <= $bar_y_bottom_coord} {incr i 25} {
        $vbar create line [expr {$bar_x_left_coord - 5}] $i $bar_x_left_coord $i -fill white
   }


   #--------------------------------------------
   # Create the text to be displayed beside the line
   #--------------------------------------------

   set minimum [expr {int($bar_min($elem_num)) }]
   set maximum [expr {int($bar_max($elem_num)) }]
   set middle [expr {int((($bar_min($elem_num) - $bar_max($elem_num))/2) + $bar_max($elem_num)) }]

   # Bar graph Minimum
   label $vbar.namebar_min -text $maximum -font {Halvetica 8 } -bg black -fg white
   place $vbar.namebar_min -x  0 -y 130

    # Bar graph Maximum
   label $vbar.namebar_max -text $minimum -font {Halvetica 8 } -bg black -fg white
   place $vbar.namebar_max -x  0 -y 35

    # Bar graph Middle
   label $vbar.namemiddle -text $middle -font {Halvetica 8 } -bg black -fg white
   place $vbar.namemiddle -x  0 -y 85

   # Bar graph Title
   label $vbar.name -text [get_element_name 0 0 $elem_num] \
   	-wraplength 50 -font {Halvetica 10 } -bg black -fg white
   place $vbar.name -x  0 -y [expr {$bar_y_top_coord - 40}]

   bar $elem_num

}



#--------------------------------------------------------------------


#--------------------------------------------------------------------


proc bar { elem_num } {

   global bar_y_top_coord bar_y_bottom_coord bar_x_left_coord bar_x_right_coord
   global bar_check_var lbar  vtext_id  bar_timer
   global bar_min bar_max

   set volt [get_bar_voltage $elem_num]

   set vbf .ventana.editor.bframe$elem_num
   set vbar .ventana.editor.bframe$elem_num.barc

   if { ![winfo exists $vbf] || $bar_check_var($elem_num) == 0} { destroy $vbf; return   }

   # Map the range according to the canvas coordinate
   # Check that bar_min and bar_max are Set and the Difference is Non Zero
   if { [ expr { ($bar_min($elem_num) - $bar_max($elem_num)) } ] } {
   	set ratio [ expr { double(($bar_y_bottom_coord - $bar_y_top_coord)) / ($bar_max($elem_num) - $bar_min($elem_num)) } ]
   } else {
      	set ratio 0
   	}
   if { $volt > $bar_min($elem_num) } {
   	set y1 [expr { $bar_y_bottom_coord - ( ( $bar_max($elem_num) - $bar_min($elem_num) ) * $ratio ) } ]
   	
   } elseif { $volt < $bar_max($elem_num) } {
   	set y1 $bar_y_bottom_coord
  	
   } else {
   	set y1 [expr { $bar_y_bottom_coord + ( ( $volt - $bar_max($elem_num) ) * $ratio) } ]
   	}

   # (Re)Draw the 'volt' line and its value
   if [winfo exists $vbar] {
   	$vbar delete $lbar($elem_num)
   	$vbar delete $vtext_id($elem_num)
   	}

   set lbar($elem_num) [ $vbar create line [expr {$bar_x_left_coord - 10}] $y1 [expr {$bar_x_right_coord + 10}] $y1 -fill blue -width 3  -arrow both ]
   set vtext_id($elem_num) [$vbar create text $bar_x_left_coord  [expr {$bar_y_bottom_coord + 10}] -text  "$volt" -fill white -font {Halvetica 10} ]

   # If the voltage is on red range, display an alarm
   #if { ( $y1 <= $bar_y_bottom_coord && $y1 >= $bar_low_rrange ) || ( $y1 >= $bar_y_top_coord && $y1 <= $bar_up_rrange ) } {
   	#set_ana_alarm 1    	
	#}
   after 1000 [list bar $elem_num]
}

proc redisplay_bar {elem_num } {

	global bar_check_var
     	global bar_timer
	global barx bary
     	#------------------------------------------------
     	# Get the x y position for the bargraph going
     	# to be redisplayed
     	# get_xy_pos [element number] [screen]
	#------------------------------------------------
       	
	set position [get_xy_pos $elem_num 1]
 	set barx($elem_num) [lindex $position 0]
 	set bary($elem_num) [lindex $position 1]
 	#puts "bar.tcl : x and y for elem_num $elem_num : $x, $y"
 	
 	set vbf .ventana.editor.bframe$elem_num
	
	if { $bar_check_var($elem_num) == 1} {
		set bar_timer($elem_num) 0
		destroy $vbf
 		display_bar_or_not  1 $elem_num
 		return
   	}
}

proc bar_detail {elem_num } {

global bar_min bar_max bar_average bar_yrange bar_rrange

toplevel .bar_detail -width 65m -height 75m
set bard .bar_detail
wm title $bard "Bar Setup"

    label $bard.bargraph_title \
        -foreground #0000f0 -height 0 -text {Bar Graph Detail}

    label $bard.max_label -height 0 -text Minimum
    label $bard.min_label -height 0 -text Maximum
    label $bard.ave_label    -height 0 -text Average
    label $bard.yrange_label -height 0 -text "Yellow Range"
    label $bard.rrange_label -height 0 -text "Red Range"

    entry $bard.max_entry    -background white -textvariable bar_max($elem_num) 
    entry $bard.min_entry    -background white -textvariable bar_min($elem_num) 
    entry $bard.ave_entry    -background white -textvariable bar_average($elem_num) 
    entry $bard.yrange_entry -background white -textvariable bar_yrange($elem_num) 
    entry $bard.rrange_entry -background white -textvariable bar_rrange($elem_num) 

    button $bard.ok \
        -text Ok -command {setup_bar $elem_num; destroy .bar_detail}
    button $bard.cancel \
        -text Cancel -command {destroy .bar_detail}

    ###################
    # SETTING GEOMETRY
    ###################

    place $bard.bargraph_title -x 15 -y 5 -anchor nw -bordermode ignore

    place $bard.max_label    -x 15 -y 40 -anchor nw -bordermode ignore
    place $bard.min_label    -x 15 -y 65 -anchor nw -bordermode ignore
    place $bard.ave_label    -x 15 -y 90 -anchor nw -bordermode ignore
    place $bard.yrange_label -x 15 -y 115 -anchor nw -bordermode ignore
    place $bard.rrange_label -x 15 -y 140 -anchor nw -bordermode ignore

    place $bard.max_entry \
        -x 100 -y 40 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard.min_entry \
        -x 100 -y 65 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard.ave_entry \
        -x 100 -y 90 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard.yrange_entry \
        -x 100 -y 115 -width 78 -height 22 -anchor nw -bordermode ignore
    place $bard.rrange_entry \
        -x 100 -y 140 -width 78 -height 22 -anchor nw -bordermode ignore

    place $bard.ok \
        -x 30 -y 175 -anchor nw -bordermode ignore
    place $bard.cancel \
        -x 80 -y 175 -anchor nw -bordermode ignore

}


proc setup_bar { elem_num } {

global bar_min bar_max bar_average bar_yrange bar_rrange

if {$bar_min($elem_num) == "" }  { set bar_min($elem_num) 0 }
if {$bar_max($elem_num) == "" }  { set bar_max($elem_num) 5 }
if {$bar_average($elem_num) == "" } { set bar_avarage($elem_num) 2.5 }
if {$bar_yrange($elem_num) == "" }  { set bar_yrange($elem_num) 1 }
if {$bar_rrange($elem_num) == "" }  { set bar_rrange($elem_num) 2 }

display_bar_or_not 1 $elem_num

# set it to c++ database
setting_bar_detail $elem_num $bar_min($elem_num) $bar_max($elem_num) $bar_average($elem_num) $bar_yrange($elem_num) $bar_rrange($elem_num)
}


