proc RedrawDivisions { canvas } {
    global ConcentrateElemPriv 

    if { ![winfo exists $canvas] } { return }

    set width [winfo width $canvas]
    if { $width < 10 } { set width [winfo reqwidth $canvas] }
    set height [winfo height $canvas]
    if { $height < 10 } { set height [winfo reqheight $canvas] }

    set w0 $ConcentrateElemPriv(weight,0)
    set w1 $ConcentrateElemPriv(weight,1)

    $canvas delete temp
    set margenx 20
    set margeny 80
  
    $canvas create line $margenx $margeny [expr $width-$margenx] $margeny \
	    -fill red -width 2  -tags temp
   
    $canvas create oval [expr $margenx-1] [expr $margeny-1] \
	    [expr $margenx+1] [expr $margeny+1] -fill black -tags temp

    $canvas create oval [expr $width-$margenx-1] [expr $margeny-1] \
	    [expr $width-$margenx+1] [expr $margeny+1] -fill black -tags temp

    set incre 1
    set factor 1
    set NE 20
    list params 0
    set ti 0
    for { set i 1 } { $i <= $NE } { incr i } { 
	set t [expr double($i)/$NE]
	set w [expr $t*$w1-(1.0-$t)*$w0]
	if { $w > 1e-10 } {
	    set factor [expr 1.0/(1.0+$w)]
	} elseif { $w < 1e-10 } {
	    set  factor [expr 1.0-$w]
	} else {
	    set factor 1.0
	}
	#if { $sense !=1 } set factor [expr 1.0/$factor]
	set ti [expr $ti+$incre]
	lappend params $ti
	set incre [expr $incre*$factor]
    }

    #for {set i 0} {$i < [llength $params]} {incr i 1} {
    #    lreplace params $i [expr [lindex $params $i]/$ti]
    #}

    set ti [lindex $params [expr [llength $params]-1]]
    set y $margeny
    foreach t $params {
	set x [expr $t/$ti*($width-2*$margenx)+$margenx]
	$canvas create line $x [expr $y-3]  $x [expr $y+3] -fill black -tags temp
    }
}

proc ConcentrateElements { } {
    global ConcentrateElemPriv
    
    if { ![info exist ConcentrateElemPriv(weight,0)] } {
	set ConcentrateElemPriv(weight,0) 0.0
    }
    if { ![info exist ConcentrateElemPriv(weight,1)] } {
	set ConcentrateElemPriv(weight,1) 0.0
    }
    
    set w .gid.concentrateelem
    if { [winfo exists $w] } return

    InitWindow $w [_ "Concentrate elements "] ConcentrateElemWindowGeom

    trace vdelete ConcentrateElemPriv(weight,0) w "RedrawDivisions $w.cWeights;#"
    trace vdelete ConcentrateElemPriv(weight,1) w "RedrawDivisions $w.cWeights;#"
    

    frame $w.frmPropagate
    checkbutton $w.frmPropagate.chk -variable ConcentrateElemPriv(Propagate) \
	-text [_ "Propagate selection"] 
    GidHelp "$w.frmPropagate" \
	[_ "Automatically select the affected lines to assign weights"]

    canvas $w.cWeights -bd 2 -relief groove -height 50
    
    frame $w.cWeights.frmWeight0
    label $w.cWeights.frmWeight0.lweight0 -text [_ "Start Weight"]
    UpDownEntry $w.cWeights.frmWeight0.udweight0 -variable ConcentrateElemPriv(weight,0)\
	    -entry-options "-relief sunken -width 6" \
	    -up-command "IncrementW 0" -down-command "DecrementW 0" \
	    -button-options "-bd 1" \
	    -up-color blue -down-color blue
    GidHelp "$w.cWeights.frmWeight0" \
	[_ "Element concentration at start:\nPositive value to concentrate\n\
            Negative to separate"]
    
    frame $w.cWeights.frmWeight1
    label $w.cWeights.frmWeight1.lweight1 -text [_ "End Weight"]
    UpDownEntry $w.cWeights.frmWeight1.udweight1 -variable ConcentrateElemPriv(weight,1)\
	    -entry-options "-relief sunken -width 6" \
	    -up-command "IncrementW 1" -down-command "DecrementW 1" \
	    -button-options "-bd 1" \
	    -up-color blue -down-color blue 
    
    GidHelp "$w.cWeights.frmWeight1" \
	[_ "Element concentration at End:\nPositive value to concentrate\n\
            Negative to separate"]
    
    set def_back [$w cget -background]
    
    frame $w.frmButtons -bg [CCColorActivo $def_back]
    button $w.frmButtons.btnApply -text [_ "Ok"] \
	    -command "ApplyCmd $w" \
	    -underline 0 -width 6
    
    button $w.frmButtons.btnclose -text [_ "Cancel"] \
	    -command ".central.s process escape ;  grab release $w ; destroy $w" \
	    -underline 0 -width 6
    
    proc ApplyCmd { w } {
	global ConcentrateElemPriv
	.central.s process $ConcentrateElemPriv(weight,0) $ConcentrateElemPriv(weight,1)
	grab release $w
	destroy $w
    }
    
    grid $w.frmPropagate -padx 5 -pady 5 -sticky new    
    #grid $w.frmPropagate.chk -sticky w
    grid columnconf $w.frmPropagate 0 -weight 1

    grid $w.cWeights -sticky ewns
    grid columnconf $w.cWeights 0 -weight 1
    grid rowconf $w.cWeights 1 -weight 1
    grid columnconf $w.frmPropagate 0 -weight 1
 
    pack $w.cWeights.frmWeight0.lweight0 $w.cWeights.frmWeight0.udweight0 -side top
    pack $w.cWeights.frmWeight1.lweight1 $w.cWeights.frmWeight1.udweight1 -side top
    grid $w.cWeights.frmWeight0 $w.cWeights.frmWeight1  -padx 5 -pady 5 -sticky w
    grid conf $w.cWeights.frmWeight1 -sticky e
    
    grid $w.frmButtons -sticky ews -columnspan 7
    grid $w.frmButtons.btnApply $w.frmButtons.btnclose -padx 5 -pady 6
    grid columnconf $w "0" -weight 1
    grid rowconfigure $w "1" -weight 1
    
    focus $w.frmButtons.btnApply

    trace var ConcentrateElemPriv(weight,0) w "RedrawDivisions $w.cWeights;#"
    trace var ConcentrateElemPriv(weight,1) w "RedrawDivisions $w.cWeights;#"
    
    RedrawDivisions $w.cWeights

    bind $w.cWeights <Configure> "RedrawDivisions $w.cWeights" 
    bind $w <Alt-c> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Escape> "tkButtonInvoke $w.frmButtons.btnclose"
    bind $w <Return> "tkButtonInvoke $w.frmButtons.btnApply"

    
    tkwait visibility $w
    grab set $w
 
}

proc IncrementW { i } {
    global ConcentrateElemPriv
    set ConcentrateElemPriv(weight,$i) [format "%4.1f" [expr $ConcentrateElemPriv(weight,$i)+0.1]]    
}

proc DecrementW { i } {
    global ConcentrateElemPriv
    set ConcentrateElemPriv(weight,$i) [format "%4.1f" [expr $ConcentrateElemPriv(weight,$i)-0.1]]        
}

