
proc APSAlertBoxHelp {} {
    return "Usage: APSAlertBox widget \n\[-parent <string>\]\t\n\[-noPack 1\]\t\n\[-packOption <list>\]\t\n\[-contextHelp <string>\]\t\n\[-errorMessage <string>\]\t\n\[-modeless 1\]\t\n\[-type {error | warning | done}\]\t\n\[-name <string>\]\t\n\[-beep {no | once | continuous | backoff}\]\t\n\[-volume <fraction>\]\t\n\[-announceWorkstion {0|1}\]\t\n\[-toneType {sr|booster|linac|par|default}\]\t\n\nCreates\t\$parent\$widget.\n\tmsg\n\tbuttonRow.ok.button\n\t"
}

proc APSAlertBox {widget args} {
    global apsContextHelp apsScriptCommand
    set parent ""
    set noPack 0
    set packOption "-side top -fill x"
    set name "Alert"
    set errorMessage "No Message Given"
    set modeless 0
    set contextHelp ""
    set type "error"
    set beep no
    set announceWorkstation 1
    set toneType default
    set volume 1
    APSStrictParseArguments {parent noPack packOption errorMessage modeless \
			 contextHelp type name beep announceWorkstation toneType volume}
    set beep [string tolower $beep]
    if {$contextHelp != ""} {
        set apsContextHelp($parent$widget) $contextHelp
    }
    set color red
    switch [string tolower $type] {
        warning {
            set color yellow
        }
        done {
            set color green
        }
    }
    
    if {$parent == ""} {
        toplevel $widget -bg $color
        wm title $widget $name
    } else {
        frame $parent$widget -bg $color
        if {!$noPack} {
            eval pack $parent$widget $packOption
        }
    }
    if [info exists apsScriptCommand] {
        set errorMessage "${apsScriptCommand}\n$errorMessage"
    } else {
        set errorMessage "$errorMessage"
    }

    label $parent$widget.label -relief ridge -text $type -bg $color -fg black
    pack $parent$widget.label -side top -fill x
    
    set tmpText [split $errorMessage "\n"]
    set lines [llength $tmpText]
    if {$lines>30 || [string length $errorMessage] > 2000} {
        APSScrolledText .msg -parent $parent$widget -width 50 -height 40
        $parent$widget.msg.text insert end "$errorMessage\n"
    } else {
        message $parent$widget.msg -relief ridge -justify center -bd 2 \
            -width 400 -text "$errorMessage\n                            "
        pack $parent$widget.msg -side top -fill both -expand 1
    }

    frame $parent$widget.buttonRow -relief raised -bd 2
    pack $parent$widget.buttonRow -side bottom -fill x

    APSButton .ok -parent $parent$widget.buttonRow -text OK \
      -command "destroy $parent$widget" \
      -highlight 1 -contextHelp \
      "Dismiss this warning and continue with application."
    
    regsub -all {"} $errorMessage {\\"} errorMessage1
    APSButton .print -parent $parent$widget.buttonRow -size small \
      -text Print -command "APSPrint -text \"$errorMessage1\"" -contextHelp \
      "Print this $type message to local printer."
    
    APSButton .save -parent $parent$widget.buttonRow -size small \
      -text "Save As..."\
      -command "APSSaveDialog .alertSave -text \"$errorMessage1\" -modal 1" \
      -contextHelp "Save this $type message to a file."

    APSButton .email -parent $parent$widget.buttonRow -size small \
      -text "Email..." \
      -command "APSEMailDialog .alertEmail -message \"$errorMessage1\" -modal 1" \
      -contextHelp "Email $type message to someone."
   # update
    tkwait visibility $parent$widget
    if {$beep!="no"} {
        global apsAlertStopBeep
        set apsAlertStopBeep($parent$widget) 0
        set apsAlertStopBeep($parent$widget.count) 0
        if {$beep=="continuous" || $beep=="backoff"} {
            set stopBeep "global apsAlertStopBeep; set apsAlertStopBeep($parent$widget) 1"
            APSButton .silent -parent $parent$widget.buttonRow -text "Silent" \
                -command $stopBeep
        }
        APSAlertBeep -beep $beep -widget $parent$widget \
            -announceWorkstation $announceWorkstation -toneType $toneType -volume $volume
        update
    }
    if {!$modeless} {
        set grabList [grab current $parent$widget]
        focus $parent$widget
        catch {grab $parent$widget}
        tkwait window $parent$widget
        catch {grab set $grabList}
    }
}    

proc APSAlertBeep {args} {
    set beep once
    set interval 1
    set widget "" 
    set announceWorkstation 1
    set toneType default
    set volume 1.0
    global apsAlertStopBeep apsScriptHost
    APSStrictParseArguments {beep interval widget announceWorkstation toneType volume}
    set soundDir /usr/local/oag/apps/sounds
    if {$beep=="no"} {
        return
    }
    if ![info exist apsAlertStopBeep($widget.count)] {
        set apsAlertStopBeep($widget.count) 0
    } 
    if ![info exist apsAlertStopBeep($widget)] {
        set apsAlertStopBeep($widget) 0
    }
    set toneType [string tolower $toneType]
    switch $toneType {
        linac -
        par -
        booster -
        sr {
            set beepType $toneType
        }
        default {
            set beepType default
        }
    } 
    if {[string length $widget] && [winfo exist $widget] && !$apsAlertStopBeep($widget)} {
        global tcl_platform
        if {$announceWorkstation && ![expr $apsAlertStopBeep($widget.count)%60]} {
            set name $apsScriptHost
            if [file exist $soundDir/$name.wav] {
                if {$tcl_platform(os) == "SunOS"} {
                    catch {exec audioplay -i -v 100 $soundDir/$name.wav &}
                } else {
                    catch {exec play -q -v $volume $soundDir/$name.wav &}
                }
            }
            after 500
        } 
        if {$tcl_platform(os) == "SunOS"} {
            catch {exec audioplay -i -v 100 $soundDir/$beepType.wav &}
        } else {
            catch {exec play -q -v $volume $soundDir/$beepType.wav &}
        }
        #bell
        incr apsAlertStopBeep($widget.count)
        if {$beep=="continuous"} {
            after [expr $interval * 1000] "APSAlertBeep -beep continuous -interval $interval -widget $widget -announceWorkstation $announceWorkstation -toneType $toneType -volume $volume"
        }
        if {$beep=="backoff"} {
            if [expr $interval>60] {
                set interval 60
            }
            after [expr int($interval * 1000)] "APSAlertBeep -beep backoff -interval [expr $interval*1.2] -widget $widget -announceWorkstation $announceWorkstation -toneType $toneType -volume $volume"
        }
    } else {
        set apsAlertStopBeep($widget) 0
        set apsAlertStopBeep($widget.count) 0
    }
    return
}



