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



proc QualifiedNodeAttrib {} {
    global ThisDemand ThisDistance ThisColour ThisPotential ThisPredecessor \
        ThisNode ThisGraph
    set ret 1
    if {[catch {expr int($ThisDemand)}]} {set ret 0}
    if {$ThisDistance!="*" && [catch {expr int($ThisDistance)}]} {set ret 0}
    if {$ThisColour!="*" && ![QualifiedNode $ThisColour]} {set ret 0}
    if {[catch {expr int($ThisPotential)}]} {set ret 0}
    if {$ThisPredecessor!="*" && (![QualifiedArc $ThisPredecessor] || \
        $ThisNode!=[$ThisGraph arc $ThisPredecessor head])} {set ret 0}
    
    if {$ret==0} {
        global StatusInfo
        set StatusInfo "invalid node attributes"
    } else {
        set StatusInfo ""
    }
    
    return $ret
}


proc SetNodeAttributes {} {
    global WDAttribs InternalWindowing DialogFrame DefPadX DefPadY \
        ActiveDialog currentNode LFrame
    
    if {$ActiveDialog=="Arc Attributes"} {DestroyActiveDialog}
    if {$ActiveDialog=="Node Attributes"} {DestroyActiveDialog}

    if {$InternalWindowing} {
        set WDAttribs $DialogFrame.labels
        if {![winfo exists $WDAttribs]} {
            frame $WDAttribs
        }
        
        RegisterDialog "Node Attributes" $WDAttribs SetNodeAttributes {}
    } else {
        set WDAttribs .labels
        if {![winfo exists $WDAttribs]} {
            toplevel $WDAttribs
            wm title $WDAttribs "Node Attributes"
            wm resizable $WDAttribs 0 0
        } else {return}
    }
    

    UploadLabels
    if {$currentNode==""} {set currentNode 0}
    DownloadNodeLabels $currentNode

    set LFrame $WDAttribs.lframe
    frame $LFrame
     
    label $LFrame.indexText -text "Node 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 ThisNode -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.demandText -text "Demand" -anchor w
    grid $LFrame.demandText -in $LFrame \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LFrame.demand -textvariable ThisDemand -bg white -width 10
    grid $LFrame.demand -in $LFrame \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LFrame.distanceText -text "Distance Label" -anchor w
    grid $LFrame.distanceText -in $LFrame \
        -row 2 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LFrame.distance -textvariable ThisDistance -bg white -width 10
    grid $LFrame.distance -in $LFrame \
        -row 2 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LFrame.colourText -text "Colour" -anchor w
    grid $LFrame.colourText -in $LFrame \
        -row 3 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LFrame.colour -textvariable ThisColour -bg white -width 10
    grid $LFrame.colour -in $LFrame \
        -row 3 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LFrame.potentialText -text "Potential" -anchor w
    grid $LFrame.potentialText -in $LFrame \
        -row 4 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LFrame.potential -textvariable ThisPotential -bg white -width 10
    grid $LFrame.potential -in $LFrame \
        -row 4 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $LFrame.predecessorText -text "Predecessor" -anchor w
    grid $LFrame.predecessorText -in $LFrame \
        -row 5 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    entry $LFrame.predecessor -textvariable ThisPredecessor -bg white -width 10
    grid $LFrame.predecessor -in $LFrame \
        -row 5 -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.demand}
    bind $LFrame.demand      <Return> {focus $LFrame.distance}
    bind $LFrame.distance    <Return> {focus $LFrame.colour}
    bind $LFrame.colour      <Return> {focus $LFrame.potential}
    bind $LFrame.potential   <Return> {focus $LFrame.predecessor}
    bind $LFrame.predecessor <Return> {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            global ThisNode ThisGraph
            incr ThisNode
            if {$ThisNode==[$ThisGraph #nodes]} {
                set ThisNode 0
            }
            DownloadNodeLabels $ThisNode
        }
        focus $LFrame.index
    }

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

        DownloadNodeLabels $ThisNode
    }

    bind $LFrame.index <Up> {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            global ThisNode ThisGraph
            incr ThisNode -1
            if {$ThisNode==-1} {
                set ThisNode [expr {[$ThisGraph #nodes]-1}]
            }
            DownloadNodeLabels $ThisNode
        }
    }
    
    bind $LFrame.index <Down> {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            global ThisNode ThisGraph
            incr ThisNode
            if {$ThisNode==[$ThisGraph #nodes]} {
                set ThisNode 0
            }
            DownloadNodeLabels $ThisNode
        }
    }
    
    frame $WDAttribs.buttons
    button $WDAttribs.buttons.apply -text "Apply" -command {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            InitFile
        }
    }
    
    button $WDAttribs.buttons.arcs -text "Arcs" -command {
        if {[QualifiedNodeAttrib]} {
            source [file join $GoblinTclPath editArcLabels.tk]
        }
    }
    
    button $WDAttribs.buttons.done -text "Done" -command {
        if {[QualifiedNodeAttrib]} {
            UploadLabels
            if {$InternalWindowing} {
                DestroyActiveDialog
            } else {
                destroy $WDAttribs
            }
            InitFile
        }
    }
    
    pack $WDAttribs.buttons.apply $WDAttribs.buttons.arcs \
        $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
}

SetNodeAttributes
