#!/usr/bin/wish
# $Source: /cvs/X/X12SA/App/scripts/X_X12SA_bpm-grid,v $
# $Date: 2006/08/10 13:40:14 $

package require BLT
package require Epics

regsub -all {\$} {X_X06SA_bpm-grid by Elke Zimoch
$Source: /cvs/X/X12SA/App/scripts/X_X12SA_bpm-grid,v $
$Revision: 1.1 $
$Date: 2006/08/10 13:40:14 $} {} Version

if {$argc < 1} {
    puts stdout "usage: X_X06SA_bpm-grid \[-?|-v|<X-Channel> <Y-Channel>\]\n"
    exit
} elseif {[lsearch "-h -H -? -Help -help" [lindex $argv 0]] != -1} {
    puts stdout "usage: X_X06SA_bpm-grid \[-?|-v|<X-Channel> <Y-Channel>\]\n"
    exit
} elseif {[lsearch "-v -V -version -Version" [lindex $argv 0]] != -1} {
    puts stdout $Version
    exit
} elseif {$argc == 2} {
    set xChannel [lindex $argv 0]
    set yChannel [lindex $argv 1]
} else {
    puts stdout "usage: X_X06SA_bpm-grid \[-?|-v|<X-Channel> <Y-Channel>\]\n"
    exit
}


proc setpos {xy channel io val sevr stat time} {
    global Egu NormalBG
    if ![info exists Egu(${xy})] {
        set Egu(${xy}) [pvget ${channel}.EGU]
    }
    if {$io != "OK" || $sevr == "INVALID"} {
        .g element config bpm -hide yes
        .${xy} config -text [format "${xy}: %s" "INVALID"]
        set sevr "INVALID"
    } else {
        .${xy} config -text [format "${xy}: %5.3f %s" $val $Egu(${xy})]
        if {($Egu(${xy}) == "micron") || ($Egu(${xy}) == "m")} {set val [expr $val/100.0]}
        .g element config bpm -hide no -${xy}data $val
    }
    switch $sevr {
        "NO_ALARM" {.${xy} config -bg $NormalBG}
        "MINOR"    {.${xy} config -bg yellow}
        "MAJOR"    {.${xy} config -bg red}
        "INVALID"  {.${xy} config -bg white}
    }
}

proc movemark {X Y} {
    foreach {x y} [.g invtransform $X $Y] {}
    .g element config mark -xdata $x -ydata $y
}

proc ShowMarker {} {
    global Marker
    if ![info exists Marker] {
        .g element create mark -xdata 0 -ydata 0 -color blue
        .g element bind mark <B1-Motion> {movemark %x %y}
        set Marker 1
    }
    if {$Marker == 1} {
        .g element config mark -hide no
        .marker config -text "Hide Marker"
        set Marker 0
    } else {
        .g element config mark -hide yes
        .marker config -text "Show Marker"
        set Marker 1
    }
}

proc ZoomIn {} {
    .g xaxis config -min -0.11 -max 0.11 -stepsize 0.02
    .g yaxis config -min -0.11 -max 0.11 -stepsize 0.02
    .zoom config -text "Zoom Out" -command ZoomOut
}

proc ZoomOut {} {
    .g xaxis config -min -5 -max 5 -stepsize 1
    .g yaxis config -min -5 -max 5 -stepsize 1
    .zoom config -text "Zoom In" -command ZoomIn
}

proc InitWindow {xString yString} {
    button .zoom -text "Zoom Out" -command ZoomOut
    button .marker -text "Show Marker" -command ShowMarker
    grid .zoom .marker -row 0
    blt::graph .g -width 400 -height 400
    .g grid on
    .g legend config -hide yes
    .g xaxis config -min -0.11 -max 0.11 -stepsize 0.02
    .g yaxis config -min -0.11 -max 0.11 -stepsize 0.02
    .g marker create line -coor {0 -10 0 10} -linewidth 2 -under yes
    .g marker create line -coor {-10 0 10 0} -linewidth 2 -under yes
    .g element create bpm -color green
    grid .g - -row 1
    label .xString -text $xString
    label .yString -text $yString
    grid .xString .yString -row 2
    label .x
    label .y
    grid .x .y -row 3
}

InitWindow $xChannel $yChannel
set NormalBG [. cget -bg]
pvmon $xChannel "setpos x $xChannel"
pvmon $yChannel "setpos y $yChannel"
