# ***************************************************************************
# * FC2 PACKAGE SOURCE FILE
# * 
# * xeve-result.tcl -- Tcl Procedures for the Result Management
# ***************************************************************************
proc DisplayReachableStates {} {
    global symbolicName
    global blifName

    set w .reachable_states
    if {[ShowResultPanel $w]} {
        return
    }
    
    toplevel $w
    wm withdraw $w
    wm group $w .
    update

    wm minsize $w 450 250
    wm maxsize $w 800 600
    wm geometry $w 450x250
    wm title $w "Reachable States"

    BuildKernelPanel $w

    $w.frame1.button7 configure -state normal

    wm deiconify $w
    raise $w .
    update idletask
}

proc DisplayCheckOutput {} {
    global symbolicName
    global blifName

    set w .checkoutput
    if {[ShowResultPanel $w]} {
        return
    }
    toplevel $w
    wm withdraw $w
    wm group $w .
    update

    wm minsize $w 650 495
    wm maxsize $w 800 600
    wm geometry $w 650x495
    wm title $w "Check Output Results"

    BuildKernelPanel $w

    $w.frame1.button7 configure -state normal

    BuildCheckOutputPanel

    wm deiconify $w
    SetCheckOutputBalloonHelp
    raise $w .
    update idletask
}

proc DisplayReduce {} {
    global symbolicName
    global blifName

    set w .reduce

    if {[ShowResultPanel $w]} {
        return
    }
    toplevel $w
    wm withdraw $w
    wm group $w .
    update

    wm minsize $w 640 270
    wm geometry $w 650x280
    wm maxsize $w 700 400
    wm title $w "Fsm Minimisation Results"

    BuildKernelPanel $w

    $w.frame1.button7 configure -state normal

    AddReducePanel

    wm deiconify $w
    SetReduceBalloonHelp
    raise $w .
    update idletask
}

proc BuildKernelPanel {w} {
    global symbolicName
    global blifName
    
    # build widget $w.frame1
    frame $w.frame1 \
            -borderwidth {2} \
            -relief {sunken}

    # build widget $w.frame1.button6
    button $w.frame1.button6 \
            -text {Dismiss} \
            -command "CloseResultPanel $w" \
            -padx 3m \
            -pady 3m \
            -state {disabled}

    # build widget $w.frame1.button7
    button $w.frame1.button7 \
            -text {Abort} \
            -command "abort_computation" \
            -padx 3m \
            -pady 3m

    # pack .frame1
    pack $w.frame1.button6 -expand 1 -pady 3m -side left
    pack $w.frame1.button7 -expand 1 -pady 3m -side right

    # build widget $w..label1
    label $w.label1 \
            -text {FSM Sizes} \
            -relief flat \
            -anchor w

    # build widget $w.frame7
    frame $w.frame7 -borderwidth {2} -relief groove

    frame $w.frame7.frame1 -borderwidth 2

    # build widget $w.frame7.frame1.label1
    label $w.frame7.frame1.label1 \
            -text {Reachable States} \
            -anchor e

    # build widget $w.frame7.frame1.label2
    label $w.frame7.frame1.label2 \
            -relief {sunken} \
            -text {} \
            -background {#ffffff} \
            -width {15}

    pack $w.frame7.frame1.label1 -side left
    pack $w.frame7.frame1.label2 -side left

    pack $w.frame7.frame1 -side left

    frame $w.frame3 \
            -borderwidth 2 \
            -relief {sunken}

    # build widget $w.frame3.message1
    message $w.frame3.message1 \
            -aspect {2000} \
            -relief {flat} \
            -text {} \
            -anchor w \
            -foreground {#ff0000}

    # build widget $w.frame3.message1
    message $w.frame3.message2 \
            -aspect {1700} \
            -relief {flat} \
            -text {} \
            -anchor w \
            -foreground {#0000ff}

    pack $w.frame3.message1 -side top -fill x -padx 3m
    pack $w.frame3.message2  -side top -fill both -padx 3m -pady 3m

    pack $w.frame1 -side bottom -fill x -padx 3m -pady 3m
    pack $w.frame3 -side top -fill x -padx 3m -pady 3m
    pack $w.label1 -side top -fill x -padx 3m
    pack $w.frame7 -side top -fill x -ipady 5m -ipadx 3m -padx 3m

}

proc BuildCheckOutputPanel {} {
    global symbolicName
    global blifName

    set w .checkoutput

    wm deiconify $w
    update

    # build widget $w.label2
    label $w.label2 -text "Output Checking Results"

    frame $w.frame5 \
            -borderwidth 2 \
            -relief flat

    scrollbar $w.frame5.vscroll \
            -relief sunken \
            -command "$w.frame5.signals yview"

    scrollbar $w.frame5.hscroll \
            -orient horiz \
            -relief sunken \
            -command "$w.frame5.signals xview"

    listbox $w.frame5.signals \
            -background #ffffff \
            -exportselection false \
            -relief groove \
            -xscrollcommand "$w.frame5.hscroll set" \
            -yscrollcommand "$w.frame5.vscroll set"

    bind $w.frame5.signals <Button1-Motion> \
            "SelectCheckOutputSignal %W %y"
    bind $w.frame5.signals <Shift-Button1-Motion> \
            "SelectCheckOutputSignal %W %y"
    bind $w.frame5.signals <ButtonPress-1> "SelectCheckOutputSignal %W %y"

    pack $w.frame5.vscroll -side right -fill y
    pack $w.frame5.signals -side top -expand 1 -fill both
    pack $w.frame5.hscroll -side top -fill x

    # build widget $w.frame18
    frame $w.frame18 \
            -borderwidth {2}

    # build widget $w.frame18.output_lab
    label $w.frame18.output_lab \
            -text {Output Name:}

    # build widget $w.frame18.output
    entry $w.frame18.output \
            -state disabled \
            -background #ffffff \
            -relief {sunken} \
            -width {35} \
            -foreground {#ff0000}

    # build widget $w.frame18.status_lab
    label $w.frame18.status_lab \
            -text {Status:}

    # build widget $w.frame18.status
    entry $w.frame18.status \
            -state disabled \
            -background #ffffff \
            -relief {sunken} \
            -width {35} \
            -foreground {#ff0000}

    # build widget $w.frame2.file_lab
    label $w.frame18.file_lab \
            -text {Path saved in file:}

    # build widget $w.frame2.file
    entry $w.frame18.file \
            -state disabled \
            -background #ffffff \
            -relief {sunken} \
            -width {35} \
            -foreground {#ff0000}

    pack $w.frame18.output_lab -expand 1 -anchor w -ipady 1m
    pack $w.frame18.output -expand 1 -anchor w -ipady 1m
    pack $w.frame18.status_lab -expand 1 -anchor w -ipady 1m
    pack $w.frame18.status -expand 1 -anchor w -ipady 1m
    pack $w.frame18.file_lab -expand 1 -anchor w -ipady 1m
    pack $w.frame18.file -expand 1 -anchor w -ipady 1m

    pack $w.frame18 -side right -expand 1  -fill both \
            -before $w.frame1 -after $w.frame7
    pack $w.frame5 -side left -expand 1 -fill both \
            -before $w.frame1 -after $w.frame7
    pack $w.label2 -fill x \
            -before $w.frame18 -after $w.frame7

    update
}

proc AddReducePanel {} {
    global symbolicName
    global blifName

    set w .reduce

    # build widget $w.frame1.button5
    button $w.frame1.button5 \
            -text {DRAW AUTOMATON} \
            -command {DrawAutomaton $blifName(SavedFc2File)} \
            -padx 3m \
            -pady 3m \
            -state {disabled}

    pack $w.frame1.button5 -side left -before $w.frame1.button6 \
            -expand 1 -pady 3m

    frame $w.frame7.frame2 \
            -borderwidth {2}

    # build widget $w.frame7.frame2.label1
    label $w.frame7.frame2.label1 \
            -text {Minimized States} \
            -anchor w

    # build widget $w.frame7.frame2.label2
    label $w.frame7.frame2.label2 \
            -relief {sunken} \
            -text {} \
            -background {#ffffff} \
            -width {15}

    pack $w.frame7.frame2.label2 -side right
    pack $w.frame7.frame2.label1 -side right

    # pack widget $w.frame7
    pack $w.frame7.frame2 -side right

}

proc CloseResultPanel {w} {
    global symbolicName

    wm withdraw $w
    update
}

proc ShowResultPanel {w} {
    global symbolicName

    if {[winfo exists $w]} {
        wm deiconify $w
        raise $w .
        update
        return 1
    }
    return 0
}

proc DismissResultPanel {w} {

    if {[winfo exists $w]} {
        destroy $w
        update
    }
}

proc DisableResultButtons {w} {
    global symbolicName

    $w$symbolicName(Dismiss) configure -state disabled
    $w$symbolicName(AbortButton) configure -state normal
    $w$symbolicName(WhatMsg) configure -text ""
    $w$symbolicName(EndMsg) configure -text ""
    if {$w == ".reduce"} {
        $w$symbolicName(DrawAutomaton) configure -state disabled
    }
    update
}

# Procedure: CheckOutpoutMessage
proc SetAndDisplay {outputName status {file none}} {
    global symbolicName
    global blifName

    AdjustOutputSelection $outputName
    set blifName($outputName,status) $status

    set blifName($outputName,file) [file tail $file]
    SetCheckOutputNameRecord $outputName
    update
}

# Procedure: AdjustOutputSelection
proc AdjustOutputSelection {outputName} {
    global blifName
    global symbolicName

    set w .checkoutput
    set list [$w$symbolicName(CheckOutputList) get 0 end]
    set index [$w$symbolicName(CheckOutputList) curselection]
    if {$index != ""} {
        $w$symbolicName(CheckOutputList) selection clear $index $index
    }
    set index [lsearch -exact $list $outputName]
    $w$symbolicName(CheckOutputList) selection set $index $index
    $w$symbolicName(CheckOutputList) see $index
}

proc SelectCheckOutputSignal {list y} {
    global blifName
    global symbolicName

    set w .checkoutput
    set index [$list nearest $y]
    if {$index >= 0} {
        $list selection set $index $index
    }
    set outputName [$list get $index]
    SetCheckOutputNameRecord $outputName
}

proc SetCheckOutputNameRecord {outputName} {
    global blifName
    global symbolicName

    set w .checkoutput
    # $w$symbolicName(CheckOutputName) configure -text $outputName
    # $w$symbolicName(CheckOutputFile) configure \
            #       -text $blifName($outputName,file)
    # $w$symbolicName(CheckOutputStatus) configure \
            #       -text $blifName($outputName,status)

    $w$symbolicName(CheckOutputName) configure -state normal
    $w$symbolicName(CheckOutputName) delete 0 end
    $w$symbolicName(CheckOutputName) insert 0 $outputName
    $w$symbolicName(CheckOutputName) configure -state disabled

    $w$symbolicName(CheckOutputFile) configure -state normal
    $w$symbolicName(CheckOutputFile) delete 0 end
    $w$symbolicName(CheckOutputFile) insert 0 $blifName($outputName,file)
    $w$symbolicName(CheckOutputFile) configure -state disabled

    $w$symbolicName(CheckOutputStatus) configure -state normal
    $w$symbolicName(CheckOutputStatus) delete 0 end
    $w$symbolicName(CheckOutputStatus) insert 0 $blifName($outputName,status)
    $w$symbolicName(CheckOutputStatus) configure -state disabled

    update
}


