
#-------------------------------------------------------------------------------
# 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 WDAttribs InternalWindowing DialogFrame DefPadX DefPadY \
        ActiveDialog currentArc LFrame
    
    if {$ActiveDialog=="Node Attributes"} {DestroyActiveDialog}
    if {$ActiveDialog=="Arc Attributes"} {DestroyActiveDialog}

    if {$InternalWindowing} {
        set WDAttribs $DialogFrame.labels
        if {![winfo exists $WDAttribs]} {
            frame $WDAttribs
        }
        
        RegisterDialog "Arc Attributes" $WDAttribs SetArcAttributes {}
    } else {
        set WDAttribs .labels
        if {![winfo exists $WDAttribs]} {
            toplevel $WDAttribs
            wm title $WDAttribs "Arc Attributes"
            wm resizable $WDAttribs 0 0
        } else {return}
    }
    
    UploadLabels
    if {$currentArc==""} {set currentArc 0}
    DownloadArcLabels $currentArc

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

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

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

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

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

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

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

    bind $LFrame.index <FocusOut> {
        if {![QualifiedArc $ThisArc]} {set ThisArc 0}

        DownloadArcLabels $ThisArc
    }

    bind $LFrame.index <Up> {
        if {[QualifiedArcAttrib]} {
            UploadLabels
            global ThisArc ThisGraph
            incr ThisArc -1
            if {$ThisArc==-1} {
                set ThisArc [expr {[$ThisGraph #arcs]-1}]
            }
            DownloadArcLabels $ThisArc
        }
    }
    
    bind $LFrame.index <Down> {
        if {[QualifiedArcAttrib]} {
            UploadLabels
            global ThisArc ThisGraph
            incr ThisArc
            if {$ThisArc==[$ThisGraph #arcs]} {
                set ThisArc 0
            }
            DownloadArcLabels $ThisArc
        }
    }
    
    frame $WDAttribs.buttons
    button $WDAttribs.buttons.apply -text "Apply" -command {
        if {[QualifiedArcAttrib]} {
            UploadLabels
            InitFile
        }
    }
    
    button $WDAttribs.buttons.nodes -text "Nodes" -command {
        if {[QualifiedArcAttrib]} {
            source [file join $GoblinTclPath editNodeLabels.tk]
        }
    }
    
    button $WDAttribs.buttons.done -text "Done" -command {
        if {[QualifiedArcAttrib]} {
            UploadLabels
            if {$InternalWindowing} {
                DestroyActiveDialog
            } else {
                destroy $WDAttribs
            }
            InitFile
        }
    }
    
    pack $WDAttribs.buttons.apply $WDAttribs.buttons.nodes \
        $WDAttribs.buttons.done -side left -fill x -padx 3m
     
    pack $WDAttribs.buttons -padx 3m -pady 3m -side bottom

    if {$InternalWindowing} {
        pack $WDAttribs -anchor nw
        AlignActiveDialog
    }
    
    focus $LFrame.index
}

SetArcAttributes
