
#-------------------------------------------------------------------------------
# Module:     editArcLabels.tk
# Activation: Dynamically loaded on user request
# Scope:      Dialog for editing arc labels
#-------------------------------------------------------------------------------



proc QualifiedArcAttrib {} {
    global ThisLength ThisUpper ThisLower ThisSubgraph StatusInfo
    set ret 1
    
    if {$ThisLength!="*" && [catch {expr int($ThisLength)}]} {set ret 0}
    if {$ThisUpper!="*" && [catch {expr int($ThisUpper)}]} {set ret 0}
    if {$ret==1 && [expr {$ThisUpper<0}]} {set ret 0}
    if {[catch {expr int($ThisLower)}] || [expr {$ThisLower<0}]} {set ret 0}
    if {$ret==1 && $ThisUpper!="*" && [expr $ThisLower>$ThisUpper]} {set ret 0}
    if {[catch {expr int($ThisSubgraph)}]} {set ret 0}
    if {$ret==1 && $ThisUpper!="*" && [expr $ThisSubgraph>$ThisUpper]} {
        set ret 0
        set ThisSubgraph $ThisUpper
    }

    if {$ret==1 && [expr {$ThisSubgraph<$ThisLower}]} {
        set ret 0
        set ThisSubgraph $ThisLower
    }
    
    if {$ret==0} {
        set StatusInfo "invalid arc attributes"
    } else {
        set StatusInfo ""
    }
    
    return $ret
}



proc SetArcAttributes {} {
    global WDArcAttribs DefPadX currentArc ThisArc LArcFrame RegisteredPath
    
    set title "Arc Attributes"

    if {[RegisterFormOrWindow $title arcLabels WDArcAttribs SetArcAttributes \
            "Done"          {
                if {[QualifiedArcAttrib]} {
                    UploadArcLabels
                    InitFile
                } else {
                    return
                }
            } \
            "Apply"         {
                if {[QualifiedArcAttrib]} {
                    UploadArcLabels
                    InitFile
                }
            } \
            "Nodes"         {
                if {[QualifiedArcAttrib]} {
                    source [file join $GoblinTclPath editNodeLabels.tk]
                }
            } \
            ""              {} \
        ]
    } {
        UploadArcLabels
        DownloadArcLabels $ThisArc
        focus $LArcFrame.index
        return
    }

    
    set LArcFrame $WDArcAttribs.lframe
    frame $LArcFrame
     
    label $LArcFrame.indexText -text "Arc Index:" -anchor w
    grid $LArcFrame.indexText -in $LArcFrame \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m -pady 2m
    entry $LArcFrame.index -textvariable ThisArc -width 5 -bg white
    grid $LArcFrame.index -in $LArcFrame \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m -pady 2m

    label $LArcFrame.lowerText -text "Lower Bound:" -anchor w
    grid $LArcFrame.lowerText -in $LArcFrame \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LArcFrame.lower -textvariable ThisLower -bg white -width 10
    grid $LArcFrame.lower -in $LArcFrame \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LArcFrame.upperText -text "Upper Bound:" -anchor w
    grid $LArcFrame.upperText -in $LArcFrame \
        -row 2 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LArcFrame.upper -textvariable ThisUpper -bg white -width 10
    grid $LArcFrame.upper -in $LArcFrame \
        -row 2 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LArcFrame.lengthText -text "Length:" -anchor w
    grid $LArcFrame.lengthText -in $LArcFrame \
        -row 3 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LArcFrame.length -textvariable ThisLength -bg white -width 10
    grid $LArcFrame.length -in $LArcFrame \
        -row 3 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LArcFrame.subgraphText -text "Subgraph:" -anchor w
    grid $LArcFrame.subgraphText -in $LArcFrame \
        -row 4 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LArcFrame.subgraph -textvariable ThisSubgraph -bg white -width 10
    grid $LArcFrame.subgraph -in $LArcFrame \
        -row 4 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    grid rowconfig    $LArcFrame 0 -weight 1 -minsize 0
    grid columnconfig $LArcFrame 0 -weight 1 -minsize 0

    pack $LArcFrame -side top -padx $DefPadX -pady 5m
    
    bind $LArcFrame.index    <Return> {focus $LArcFrame.lower}
    bind $LArcFrame.lower    <Return> {focus $LArcFrame.upper}
    bind $LArcFrame.upper    <Return> {focus $LArcFrame.length}
    bind $LArcFrame.length   <Return> {focus $LArcFrame.subgraph}
    bind $LArcFrame.subgraph <Return> {
        if {[QualifiedArcAttrib]} {
            UploadArcLabels
            incr ThisArc
            if {$ThisArc==[$ThisGraph #arcs]} {
                set ThisArc 0
            }
            DownloadArcLabels $ThisArc
        }
        focus $LArcFrame.index
    }

    bind $LArcFrame.index <FocusOut> {
        if {![QualifiedArc $ThisArc]} {
            focus $LArcFrame.index
        } else {
            DownloadArcLabels $ThisArc
        }
    }

    bind $LArcFrame.index <Up> {
        if {[QualifiedArcAttrib]} {
            UploadArcLabels
            incr ThisArc -1
            if {$ThisArc==-1} {
                set ThisArc [expr {[$ThisGraph #arcs]-1}]
            }
            DownloadArcLabels $ThisArc
        }
    }
    
    bind $LArcFrame.index <Down> {
        if {[QualifiedArcAttrib]} {
            UploadArcLabels
            incr ThisArc
            if {$ThisArc==[$ThisGraph #arcs]} {
                set ThisArc 0
            }
            DownloadArcLabels $ThisArc
        }
    }
    
    focus $LArcFrame.index


    DisplayForm $title
}

SetArcAttributes
