
#-------------------------------------------------------------------------------
# Module:     layoutNodes.tk
# Activation: Dynamically loaded on user request
# Scope:      Configuration of graph node display
#-------------------------------------------------------------------------------



proc ShowNDisplayOptions {} {
    global WDNodeDisplay DefPadX ThisGraph UploadData

    set title "Node Display Options"
    set updateOnly true

    set UploadData(nodeLabelFormat) [$ThisGraph layout info -nodeLabelFormat]
    set UploadData(nodeFontSize) [$ThisGraph layout info -nodeFontSize]
    set UploadData(nodeSize) [$ThisGraph layout info -nodeSize]

    if {![RegisterFormOrWindow $title nodedisplay WDNodeDisplay ShowNDisplayOptions \
            "Done"          {
                UpdateNodeLayoutParameters
                PropagateModifications
            } \
            "Apply"         {
                UpdateNodeLayoutParameters
                PropagateModifications
            } \
            ""              {} \
            ""              {} \
        ]
    } {
        set updateOnly false

        frame $WDNodeDisplay.options
        pack $WDNodeDisplay.options -fill both -padx $DefPadX -pady 3m


        set rowNr 0

        label $WDNodeDisplay.options.nodeLabelLabel -text "Label Selection: " \
            -anchor w
        grid $WDNodeDisplay.options.nodeLabelLabel \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m

        MakePulldownMenuButton $WDNodeDisplay.options.nodeLabelSelect "-width 15"
        grid $WDNodeDisplay.options.nodeLabelSelect \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m


        incr rowNr

        label $WDNodeDisplay.options.formatStringLabel -text "Format String: " \
            -anchor w
        grid $WDNodeDisplay.options.formatStringLabel \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
        entry $WDNodeDisplay.options.formatStringString \
            -textvariable UploadData(nodeLabelFormat) -bg white -width 15
        grid $WDNodeDisplay.options.formatStringString \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m

        bind $WDNodeDisplay.options.formatStringString <FocusOut> {
            if {[UpdateLayoutStringValue nodeLabelFormat]} {
                PropagateModifications
            }
        }

        bind $WDNodeDisplay.options.formatStringString <Return> {
            if {[UpdateLayoutStringValue nodeLabelFormat]} {
                PropagateModifications
            }
            focus $WDNodeDisplay.options.fontSizeFrame.value
        }


        incr rowNr

        label $WDNodeDisplay.options.fontTypeLabel -text "Font Type: " \
            -anchor w
        grid $WDNodeDisplay.options.fontTypeLabel -sticky news -padx 1m \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1

        MakePulldownMenuButton $WDNodeDisplay.options.nodeFontSelect "-width 12"
        grid $WDNodeDisplay.options.nodeFontSelect -sticky news -padx 1m \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1


        incr rowNr

        label $WDNodeDisplay.options.fontSizeLabel -text "Font Size: " \
            -anchor w
        grid $WDNodeDisplay.options.fontSizeLabel -sticky news -padx 1m \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1

        frame $WDNodeDisplay.options.fontSizeFrame
        grid $WDNodeDisplay.options.fontSizeFrame -sticky news -padx 1m \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1

        entry $WDNodeDisplay.options.fontSizeFrame.value \
            -textvariable UploadData(nodeFontSize) -bg white -width 5
        label $WDNodeDisplay.options.fontSizeFrame.label -text " % of Default Size" \
            -anchor w

        pack $WDNodeDisplay.options.fontSizeFrame.label -side right
        pack $WDNodeDisplay.options.fontSizeFrame.value -expand true -fill both -side left

        bind $WDNodeDisplay.options.fontSizeFrame.value <FocusOut> {
            if {[UpdateLayoutNumericValue nodeFontSize]} {
                PropagateModifications
            }
        }

        bind $WDNodeDisplay.options.fontSizeFrame.value <Return> {
            if {[UpdateLayoutNumericValue nodeFontSize]} {
                PropagateModifications
            }

            focus $WDNodeDisplay.options.fontSizeFrame.value
        }


        incr rowNr

        frame $WDNodeDisplay.options.separator$rowNr -height 3m
        grid $WDNodeDisplay.options.separator$rowNr \
            -row $rowNr -column 0 -rowspan 1 -columnspan 2 -sticky news -padx 1m


        incr rowNr

        label $WDNodeDisplay.options.nodeShapeLabel -text "Node Style: " -anchor w
        grid $WDNodeDisplay.options.nodeShapeLabel \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m

        MakePulldownMenuButton $WDNodeDisplay.options.nodeShapeSelect "-width 12"
        grid $WDNodeDisplay.options.nodeShapeSelect \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m


        incr rowNr

        label $WDNodeDisplay.options.nodeSizeLabel -text "Node Size: " \
            -anchor w
        grid $WDNodeDisplay.options.nodeSizeLabel -sticky news -padx 1m \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1

        frame $WDNodeDisplay.options.nodeSizeFrame
        grid $WDNodeDisplay.options.nodeSizeFrame -sticky news -padx 1m \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1

        entry $WDNodeDisplay.options.nodeSizeFrame.value -textvariable UploadData(nodeSize) -bg white -width 5
        label $WDNodeDisplay.options.nodeSizeFrame.label1 -text " % of Default Size" \
            -anchor w

        pack $WDNodeDisplay.options.nodeSizeFrame.label1 -side right
        pack $WDNodeDisplay.options.nodeSizeFrame.value -expand true -fill both -side left

        bind $WDNodeDisplay.options.nodeSizeFrame.value <FocusOut> {
            if {[UpdateLayoutNumericValue nodeSize]} {
                PropagateModifications
            }
        }

        bind $WDNodeDisplay.options.nodeSizeFrame.value <Return> {
            if {[UpdateLayoutNumericValue nodeSize]} {
                PropagateModifications
            }

            focus $WDNodeDisplay.options.formatStringString
        }


        incr rowNr

        label $WDNodeDisplay.options.nodeColourLabel -text "Node Colours: " \
            -anchor w
        grid $WDNodeDisplay.options.nodeColourLabel \
            -row $rowNr -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m

        MakePulldownMenuButton $WDNodeDisplay.options.nodeColourSelect "-width 12"
        grid $WDNodeDisplay.options.nodeColourSelect \
            -row $rowNr -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m


        grid columnconfig $WDNodeDisplay.options 0 -weight 1 -minsize 0
        grid columnconfig $WDNodeDisplay.options 1 -weight 1 -minsize 0

        focus $WDNodeDisplay.options.formatStringString

        DisplayForm $title
    }

    set listOfNodeLabelOptions { \
        "Apply Format String"   0 \
        "No Labels"             1 \
        "Indices {0,1,2,..}"    2 \
        "Distance Labels"       3 \
        "Potentials"            4 \
        "Node Colours"          5 \
        "Demands"               6 \
        "Indices {1,2,3,..}"    7 \
    }

    AttachSelectionForm $WDNodeDisplay.options.nodeLabelSelect 5 \
        [lindex $listOfNodeLabelOptions [expr 2*[ExtractNodeLabelMode]]] \
        $listOfNodeLabelOptions ApplyNodeLabelSelection

    AttachFontTypeMenu $WDNodeDisplay.options.nodeFontSelect nodeFontType ApplyNodeFontSelection

    set listOfNodeShapeOptions { \
        "Fixed Size Dots"       0 \
        "Circular"              1 \
        "Rectangular"           2 \
        "Regular By Colours"    3 \
    }

    AttachSelectionForm $WDNodeDisplay.options.nodeShapeSelect 5 \
        [lindex $listOfNodeShapeOptions [expr 2*[$ThisGraph layout info -nodeShapeMode]]] \
        $listOfNodeShapeOptions ApplyNodeShapeSelection

    set listOfNodeColourOptions { \
        "Uncoloured Inside"         0 \
        "Highlight Distance Labels" 1 \
        "Colour Table Lookup"       2 \
        "Highlight Demand Nodes"    3 \
        "Gradient Colours"          4 \
    }

    AttachSelectionForm $WDNodeDisplay.options.nodeColourSelect 5 \
        [lindex $listOfNodeColourOptions [expr 2*[$ThisGraph layout info -nodeColourMode]]] \
        $listOfNodeColourOptions ApplyNodeColourSelection
}

proc ExtractNodeLabelMode {} {
    global ThisGraph

    if {[$ThisGraph layout info -nodeLabelFormat]==""} {return 1}

    set nodeLabelMode 0
    set num [scan [$ThisGraph layout info -nodeLabelFormat] "#%i%s" nodeLabelMode remainder]

    if {$num!=1} {return 0}

    return [expr $nodeLabelMode+1]
}

proc ApplyNodeLabelSelection {nodeLabelMode} {
    global ThisGraph

    if {$nodeLabelMode==0} {return}

    if {   $nodeLabelMode==1 \
        && [$ThisGraph layout info -nodeLabelFormat]!="" \
        && [$ThisGraph layout info -nodeLabelFormat]!="#0" \
    } {
        $ThisGraph layout configure -nodeLabelFormat ""
        PropagateModifications
        return
    }

    if {![string equal [$ThisGraph layout info -nodeLabelFormat] "#[expr $nodeLabelMode-1]"]} {
        $ThisGraph layout configure -nodeLabelFormat "#[expr $nodeLabelMode-1]"
        PropagateModifications
    }
}

proc ApplyNodeFontSelection {nodeFontType} {
    global ThisGraph WDNodeDisplay

    if {[$ThisGraph layout info -nodeFontType]!=$nodeFontType} {
        $ThisGraph layout configure -nodeFontType $nodeFontType
        UpdateFontDisplay $WDNodeDisplay.options.nodeFontSelect $nodeFontType
        PropagateModifications
    }
}

proc ApplyNodeShapeSelection {nodeShapeMode} {
    global ThisGraph

    if {[$ThisGraph layout info -nodeShapeMode]!=$nodeShapeMode} {
        $ThisGraph layout configure -nodeShapeMode $nodeShapeMode
        PropagateModifications
    }
}

proc ApplyNodeColourSelection {nodeColourMode} {
    global ThisGraph

    if {[$ThisGraph layout info -nodeColourMode]!=$nodeColourMode} {
        $ThisGraph layout configure -nodeColourMode $nodeColourMode
        PropagateModifications
    }
}

proc UpdateNodeLayoutParameters {} {
    global ThisGraph UploadData

    goblin configure -nodeLabelFormat $UploadData(nodeLabelFormat)
    UpdateLayoutStringValue  nodeLabelFormat
    UpdateLayoutNumericValue nodeFontSize
    UpdateLayoutNumericValue nodeSize
}

ShowNDisplayOptions
