
proc SelectTraceLevel {} {
    global WDTraceLevel InternalWindowing DialogFrame DefPadX DefPadY \
        ActiveDialog Fr2Options Fr1Options

    if {$ActiveDialog=="Tracing Options"} {return}

    if {$InternalWindowing} {
        set WDTraceLevel $DialogFrame.traceopt
        if {![winfo exists $WDTraceLevel]} {
            eval frame $WDTraceLevel $Fr1Options
        }
        
        RegisterDialog "Tracing Options" $WDTraceLevel SelectTraceLevel {}
    } else {
        set WDTraceLevel .traceopt
        if {![winfo exists $WDTraceLevel]} {
            toplevel $WDTraceLevel
            wm title $WDTraceLevel "Tracing Options"
            wm resizable $WDTraceLevel 0 0
        }
    }
    
    eval frame $WDTraceLevel.f1 $Fr2Options
    label $WDTraceLevel.f1.label -text "Trace Level:" -anchor w
    radiobutton $WDTraceLevel.f1.noTracing -text "No Tracing" \
        -variable goblinTraceLevel -value 0 -anchor w
    radiobutton $WDTraceLevel.f1.constructors -text "Trace Constructors" \
        -variable goblinTraceLevel -value 2 -anchor w
    radiobutton $WDTraceLevel.f1.exhaustive -text "Exhaustive Tracing" \
        -variable goblinTraceLevel -value 3 -anchor w
    pack $WDTraceLevel.f1.label $WDTraceLevel.f1.noTracing $WDTraceLevel.f1.constructors \
        $WDTraceLevel.f1.exhaustive -side top -fill x
     
    eval frame $WDTraceLevel.f2 $Fr2Options
    checkbutton $WDTraceLevel.f2.data -text "Trace Data Structures" \
        -variable goblinTraceData -anchor w
    pack $WDTraceLevel.f2.data -side left -fill x

    eval frame $WDTraceLevel.f3 $Fr2Options
     
    label $WDTraceLevel.f3.traceStepText -text "Trace Step:" -anchor w
    grid $WDTraceLevel.f3.traceStepText -in $WDTraceLevel.f3  \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    entry $WDTraceLevel.f3.traceStep -textvariable goblinTraceStep -bg white -width 5
    grid $WDTraceLevel.f3.traceStep -in $WDTraceLevel.f3 \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky w -padx 1m

    label $WDTraceLevel.f3.thresholdText -text "Threshold:" -anchor w
    grid $WDTraceLevel.f3.thresholdText -in $WDTraceLevel.f3  \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    entry $WDTraceLevel.f3.threshold -textvariable goblinThreshold -bg white -width 5
    grid $WDTraceLevel.f3.threshold -in $WDTraceLevel.f3 \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky w -padx 1m

    grid rowconfig    $WDTraceLevel.f3 0 -weight 1 -minsize 0
    grid rowconfig    $WDTraceLevel.f3 1 -weight 1 -minsize 0
    grid columnconfig $WDTraceLevel.f3 2 -weight 1 -minsize 0
    
    eval frame $WDTraceLevel.options $Fr2Options
    label $WDTraceLevel.options.label -text "Task Communication Level:" -anchor w
    radiobutton $WDTraceLevel.options.single -text "Single Thread" \
        -variable goblinCommLevel -value 0 -anchor w
    radiobutton $WDTraceLevel.options.separate -text "Separate Threads" \
        -variable goblinCommLevel -value 1 -anchor w
    radiobutton $WDTraceLevel.options.update -text "Automatic Updates" \
        -variable goblinCommLevel -value 2 -anchor w
    radiobutton $WDTraceLevel.options.trace -text "Interactive Tracing" \
        -variable goblinCommLevel -value 3 -anchor w
    pack $WDTraceLevel.options.label $WDTraceLevel.options.single \
        $WDTraceLevel.options.separate $WDTraceLevel.options.update \
        $WDTraceLevel.options.trace -side top -fill x
     
    eval frame $WDTraceLevel.value $Fr2Options
    label $WDTraceLevel.value.label -text "Update Cycle: " -anchor w
    entry $WDTraceLevel.value.value -textvariable UpdateCycle -bg white -width 5
    label $WDTraceLevel.value.label2 -text "ms" -anchor w
    pack $WDTraceLevel.value.label $WDTraceLevel.value.value $WDTraceLevel.value.label2 \
        -side left -fill x

    eval frame $WDTraceLevel.buttons $Fr1Options
    button $WDTraceLevel.buttons.restart -text "Reset" -command {
        RestartEngine
    }

    button $WDTraceLevel.buttons.done -text "Done" -command {
        if {[QualifiedInt $goblinTraceStep 1 50000] && \
            [QualifiedInt $goblinThreshold 0 50000]} {
            if {$InternalWindowing} {
                DestroyActiveDialog
            } else {
                destroy $WDTraceLevel
            }
        }
    }
    
    pack $WDTraceLevel.buttons.restart \
        $WDTraceLevel.buttons.done \
        -side left -fill x -padx 3m
     
    pack $WDTraceLevel.buttons -padx 5m -pady 5m -side bottom

    pack $WDTraceLevel.f1 $WDTraceLevel.f2 $WDTraceLevel.f3 -padx 5m -pady 5m \
        -side top -anchor w -fill x
    pack $WDTraceLevel.options $WDTraceLevel.value -padx 5m -pady 5m -side top \
        -anchor w -fill x

    if {$InternalWindowing} {
        pack $WDTraceLevel -anchor nw
        AlignActiveDialog
    }
}

if {![info exists WDTraceLevel] || ![winfo exists $WDTraceLevel]} {SelectTraceLevel}
