#!/bin/sh  
# \
exec oagwish "$0" "$@"

# $Log: not supported by cvs2svn $
# Revision 1.7  2004/02/19 17:31:37  soliday
# Changed PV names once again.
#
# Revision 1.6  2004/02/19 16:24:58  soliday
# Updated to reflect updated PV names that where changed to conform with the
# naming conventions at the beamlines.
#
# Revision 1.5  2004/02/18 22:16:18  soliday
# Changed the EPICS_CA_ADDR_LIST because the workstation that runs the
# related epics server has changed.
#
# Revision 1.4  2004/02/12 22:26:30  soliday
# Updated for the odd station ordering in 2-ID and 6-ID
#
# Revision 1.3  2004/02/12 22:19:56  soliday
# Added overview comment.
#
# Revision 1.2  2004/02/12 22:16:09  soliday
# Cleaned up the interface.
#
# Revision 1.1  2004/02/11 22:18:06  soliday
# Program to be used by floor coordinators to monitor beamline usage.
#
#

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set CVSRevisionAuthor "\$Revision: 1.8 $ \$Author: soliday $"

#set env(EPICS_CA_ADDR_LIST) "164.54.3.61"
#set env(EPICS_CA_AUTO_ADDR_LIST) NO

package require iwidgets


itcl::body iwidgets::Tabset::select {index} {
    if { $_tabs == {} } {
    error "can't activate a tab,\
        no tabs in the tabset: $itk_component(hull)"
    }
    
    # if there is not current selection just ignore trying this selection
    if { $index == "select" && $_currTab == -1 } {
        return -1
    }
    
    # is selection request in range ? 
    set reqTab [index $index]
#    if { $reqTab == -1 } {
#        error "bad value $index for index:\n        should be from 0 to [expr {[llength $_tabs] - 1}]"
#   }
    
    # If already selected then ignore and return...
    if { $reqTab == $_currTab } {
        return $reqTab
    }
    
    # ---- Deselect
    if { $_currTab != -1 } {
        set currTabName [lindex $_tabs $_currTab]
        $currTabName deselect
        
        # handle different orientations...
        if { $itk_option(-tabpos) == "n" || $itk_option(-tabpos) == "s"} {
            $currTabName configure -top $_deselectedTop
        } else {
            $currTabName configure -left $_deselectedLeft
        }
    }
    
    # get the stacking order correct...
    foreach tab $_tabs {
        $tab lower
    }
    if { $reqTab == -1 } {
        set _currTab -1
        return -1
    }
    # set this now so that the -command cmd can do an 'index select'
    # to operate on this tab.
    set _currTab $reqTab
    
    # ---- Select
    set reqTabName [lindex $_tabs $reqTab]
    $reqTabName select
    if { $itk_option(-tabpos) == "n" || $itk_option(-tabpos) == "s"} {
    $reqTabName configure -top $_selectedTop
    } else {
    $reqTabName configure -left $_selectedLeft
    }
    
    set _currTab $reqTab
    
    # invoke any user command string, appended with tab index number
    if { $_cmdStr != {} } {
    set newCmd $_cmdStr
    eval [lappend newCmd $reqTab]
    }
    
    return $reqTab
}

APSApplication . -name "Attendance Checklist" -version $CVSRevisionAuthor -overview "Used to log beamline usage statistics."
pv toggleErrorMode


proc TabSelect {index} {
    .userFrame.tnb.canvas.tabset2 select ""
    .userFrame.tnb.canvas.notebook select $index
}
proc TabSelect2 {index} {
    incr index 20
    .userFrame.tnb.canvas.tabset select ""
    .userFrame.tnb.canvas.notebook select $index
}

proc CreateWidgets {args} {
    global state state2 disputed disputed2 noted noted2 disputedList notedList
    global apsMenuBackground apsMenuForeground orientation
    set orientation w
    set orientation2 e

    
    iwidgets::tabnotebook .userFrame.tnb \
        -width 660 -height 660 \
        -tabpos $orientation \
        -backdrop DimGray \
        -raiseselect 1
    .userFrame.tnb.canvas.tabset configure \
        -command "TabSelect"
    iwidgets::tabset .userFrame.tnb.canvas.tabset2 \
        -tabpos $orientation2 \
        -backdrop DimGray \
        -raiseselect 1 \
        -command "TabSelect2"

    global tcl_platform
    if {[string range $tcl_platform(os) 0 6] == "Windows"} {
        set beamlineFile "c:/beamlines/stations.sdds"
    } else {
        set beamlineFile "/home/helios/oagData/flcoord/stations.sdds"
    }
    if {[catch {exec sdds2stream $beamlineFile -columns=Sector,Beamline,StationA,StationB,StationC,StationD,StationE,StationI,StationX} results]} {
        wm withdraw .
        APSAlertBox .alert -errorMessage "Unable to read stations.sdds file\n$results" -type error
        exit
    }
    
    global sectors
    set sectors ""
    for {set i 1} {$i <= 40} {incr i} {
        append sectors "$i "
    }
    append sectors "N "
    foreach i $sectors {
        set BM($i) ""
        set ID($i) ""
    }
    foreach "sector beamline stationA stationB stationC stationD stationE stationI stationX" $results {
        set stations ""
        if {($beamline == "ID") && ($sector == 2)} {
            set stationList "A B C E D I X"
        } elseif {($beamline == "ID") && ($sector == 6)} {
            set stationList "A D B C E I X"
        } else {
            set stationList "A B C D E I X"
        }
        foreach station $stationList {
            if {[set station${station}] == "y"} {
                append stations "$station "
            }
        }
        set ${beamline}($sector) $stations
    }

    foreach i $sectors {
        foreach n "BM ID" {
            foreach m [set ${n}($i)] {
                append pvNames "S${i}${n}${m}:MarkedAbsentBO S${i}${n}${m}:MarkedAbsentBO S${i}${n}${m}:AbsenceDisputedBO S${i}${n}${m}:AbsenceDisputedBO S${i}${n}${m}:DisputeNotedBO S${i}${n}${m}:DisputeNotedBO "
                append varNames "state(Sector$i.${n}$m) state2(Sector$i.${n}$m) disputed(Sector$i.${n}$m) disputed2(Sector$i.${n}$m) noted(Sector$i.${n}$m) noted2(Sector$i.${n}$m) "
            }
        }
    }
    pack .userFrame.tnb -side left -fill both -expand true
    pack .userFrame.tnb.canvas.tabset2 -side right -anchor nw -fill both
    update
    if {[catch {pv linkw $varNames $pvNames 30} results]} {
        wm withdraw .
        APSAlertBox .alert -errorMessage "Unable to link to PVs\n$results" -type error
        exit
    }

    foreach i $sectors {
        if {$i > 20} {
            .userFrame.tnb.canvas.tabset2 add -label "Sector $i"
            set page$i [.userFrame.tnb.canvas.notebook add -label "Sector $i"]
            set tabset .userFrame.tnb.canvas.tabset2
        } else {
            set page$i [.userFrame.tnb add -label "Sector $i"]
            set tabset .userFrame.tnb.canvas.tabset
        }

        label [set page$i].label -text "  Sector \#$i" -font "Courier 20 bold"
        pack [set page$i].label
        
        set disputedList($i) ""
        set notedList($i) ""

        pack [frame [set page$i].left] -side left -anchor n
        pack [frame [set page$i].right] -side left -anchor n

        foreach n "BM ID" side "left right" {
            foreach m [set ${n}($i)] {
                APSFrame .a${n}$m \
                    -parent [set page$i].${side} \
                    -label "$n $m" \
                    -packOption "-pady 3"
                
                set w [set page$i].${side}.a${n}$m.frame

                append disputedList($i) "disputed(Sector$i.${n}$m)  "
                append notedList($i) "noted(Sector$i.${n}$m)  "
                
                APSRadioButtonFrame .state \
                    -parent $w \
                    -label "" \
                    -variable state(Sector$i.${n}$m) \
                    -buttonList "Present Absent Unknown" \
                    -valueList "Present Absent Unknown" \
                    -packOption "-side left" \
                    -commandList "\"set state2(Sector$i.${n}$m) Present ; pv putw state2(Sector$i.${n}$m)\" \"set state2(Sector$i.${n}$m) Absent ; pv putw state(Sector$i.${n}$m)\" \"set state2(Sector$i.${n}$m) Unknown ; pv putw state(Sector$i.${n}$m)\""

                APSFrame .disputed \
                    -parent $w \
                    -label "" \
                    -packOption "-side left -anchor nw"

                label $w.disputed.frame.isdisputed -text "Not Disputed"
                pack $w.disputed.frame.isdisputed
                
                button $w.disputed.frame.noted \
                    -text "Noted" \
                    -state disabled \
                    -command "set noted2(Sector$i.${n}$m) Noted ; pv putw noted2(Sector$i.${n}$m)"
                pack $w.disputed.frame.noted -fill x

                if 0 {
                    APSButton .reset -parent [set page$i].${side}.a${n}$m \
                        -text "Reset" \
                        -size small \
                        -packOption "-fill x -expand true" \
                        -command "Reset -unit Sector$i.${n}$m"
                    [set page$i].${side}.a${n}${m}.reset.button configure -font "-adobe-courier-medium-r-normal-*-12-*-*-*-*-*-*-*"
                }

                pv umon state(Sector$i.${n}$m) \
                    "SetState -unit Sector$i.${n}$m -frame $w.state.frame"

                pv umon disputed(Sector$i.${n}$m) \
                    "SetDisputed -unit Sector$i.${n}$m -frame $w.disputed.frame -index $i -tabset $tabset"

                pv umon noted(Sector$i.${n}$m) \
                    "SetNoted -unit Sector$i.${n}$m -frame $w.disputed.frame -index $i -tabset $tabset"
                SetState \
                    -unit Sector$i.${n}$m \
                    -frame $w.state.frame \
                    -setup 1
                SetDisputed \
                    -unit Sector$i.${n}$m \
                    -frame $w.disputed.frame \
                    -index $i \
                    -tabset $tabset \
                    -setup 1
                SetNoted \
                    -unit Sector$i.${n}$m \
                    -frame $w.disputed.frame \
                    -index $i \
                    -tabset $tabset \
                    -setup 1
            }
        }
        update
    }    
    .userFrame.tnb view "Sector 1"
}

proc SetState {args} {
    set setup 0
    APSStrictParseArguments {unit frame setup}
    global state disputed2 noted2
    if {$state($unit) == "Absent"} {
        if {[$frame cget -bg] != "red"} {
            $frame configure -bg red
        }
    } elseif {$state($unit) == "Present"} {
        if {[$frame cget -bg] != "green"} {
            $frame configure -bg green
        }
        if {$setup == 0} {
            set disputed2($unit) "Reset"
            set noted2($unit) "Reset"
            if {[catch {pv putw disputed2($unit)} results]} {
            }
            if {[catch {pv putw noted2($unit)} results]} {
            }
        }
    } else {
        if {[$frame cget -bg] != "black"} {
            $frame configure -bg black
        }

    }
}

proc SetDisputed {args} {
    set setup 0
    APSStrictParseArguments {unit frame index tabset setup}
    global state disputed disputed2 disputedList noted noted2 notedList
    
    if {($disputed($unit) == "Disputed")} {
        if {($state($unit) == "Present") || ($state($unit) == "Unknown")} {
            if {$setup == 0} {
                set disputed2($unit) Reset
                if {[catch {pv putw disputed2($unit)} result]} {
                }
            }
            return
        }
    }
    if {($noted($unit) == "Noted")} {
        if {$setup == 0} {
            set noted2($unit) Reset
            if {[catch {pv putw noted2($unit)} result]} {
            }
        }
    }
    if {$disputed($unit) == "Disputed"} {
        if {[$frame cget -bg] != "red"} {
            $frame configure -bg red
        }
        if {[$frame.isdisputed cget -text] != "Is Disputed"} {
            $frame.isdisputed configure -text "Is Disputed" -fg red
            $frame.noted configure -state normal
        }
    } else {
        if {[$frame cget -bg] != "green"} {
            $frame configure -bg green
        }
        if {[$frame.isdisputed cget -text] != "Not Disputed"} {
            $frame.isdisputed configure -text "Not Disputed" -fg black
            $frame.noted configure -state disabled
        }
    }
    set yellow 0
    set red 0

    foreach n $disputedList($index) nn $notedList($index) {
        if {([set $n] == "Disputed") && ([set $nn] != "Noted")} {
            set red 1
            break
        }
        if {([set $n] == "Disputed") && ([set $nn] == "Noted")} {
            set yellow 1
        }
    }
    if {$index == "N"} {
        set index 41
    }
    incr index -1
    if {$index >= 20} {
        incr index -20
    }
    set bg [string tolower [$tabset tabcget $index -selectbackground]]

    if {$red} {
        if {$bg != "red"} {
            $tabset tabconfigure $index \
                -foreground white -selectforeground white \
                -background red -selectbackground red
        }
    } elseif {$yellow} {
        if {$bg != "yellow"} {
            $tabset tabconfigure $index \
                -foreground black -selectforeground black \
                -background yellow -selectbackground yellow
        }
    } elseif {($bg == "red") || ($bg == "yellow")} {
        $tabset tabconfigure $index \
            -foreground black -selectforeground black \
            -background Grey85 -selectbackground Grey85
    }
}

proc SetNoted {args} {
    set initialize 0
    set setup 0
    APSStrictParseArguments {unit frame index tabset setup}
    global noted disputedList disputed notedList
    if {($noted($unit) == "Noted") && ($disputed($unit) == "Disputed")} {
        if {[$frame cget -bg] != "yellow"} {
            $frame configure -bg yellow
        }
    } elseif {($noted($unit) == "Noted") && ($disputed($unit) != "Disputed")} {
        if {$setup == 0} {
            set noted2($unit) Reset
            if {[catch {pv putw noted2($unit)} result]} {
            }
        }
        return
    } elseif {$disputed($unit) == "Disputed"} {
        if {[$frame cget -bg] != "red"} {
            $frame configure -bg red
        }
    } else {
        if {[$frame cget -bg] != "green"} {
            $frame configure -bg green
        }
    }
    
    set yellow 0
    set red 0
    foreach n $disputedList($index) nn $notedList($index) {
        if {([set $n] == "Disputed") && ([set $nn] != "Noted")} {
            set red 1
            break
        }
        if {([set $n] == "Disputed") && ([set $nn] == "Noted")} {
            set yellow 1
        }
    }
    if {$index == "N"} {
        set index 41
    }
    incr index -1
    if {$index >= 20} {
        incr index -20
    }
    set bg [string tolower [$tabset tabcget $index -selectbackground]]
    if {$red} {
        if {$bg != "red"} {
            $tabset tabconfigure $index \
                -foreground white -selectforeground white \
                -background red -selectbackground red
        }
    } elseif {$yellow} {
        if {$bg != "yellow"} {
            $tabset tabconfigure $index \
                -foreground black -selectforeground black \
                -background yellow -selectbackground yellow
        }
    } elseif {($bg == "red") || ($bg == "yellow")} {
        $tabset tabconfigure $index \
            -foreground black -selectforeground black \
            -background Grey85 -selectbackground Grey85
    }
}

proc Reset {args} {
    APSStrictParseArguments {unit}
    global state2 disputed2 noted2
    set state2($unit) Present
    set disputed2($unit) Reset
    if {[catch {pv putw state2($unit)} results]} {
    }
    if {[catch {pv putw disputed2($unit)} results]} {
    }
}

proc AlertFC {args} {
    global noted disputedList disputed notedList sectors
    foreach index $sectors {
        foreach n $disputedList($index) nn $notedList($index) {
            if {([set $n] == "Disputed") && ([set $nn] != "Noted")} {
                bell
                break
            }
        }
    }
    after 10000 AlertFC
}

CreateWidgets
after 10000 AlertFC
