
#-------------------------------------------------------------------------------
# Module:     infoModules.tk
# Activation: Dynamically loaded on user request
# Scope:      Browser for algorithmic modules
#-------------------------------------------------------------------------------


# Needed for dereferencing module information

source [file join $GoblinTclPath infoTimers.tk]
source [file join $GoblinTclPath infoAuthors.tk]
source [file join $GoblinTclPath infoBibliography.tk]


proc MakeBalloonModules {label wdName} {
    global BalloonHelp
    if {!$BalloonHelp} {return}
    destroy .balloon
    toplevel .balloon
    wm geometry .balloon \
        100x20+[expr [winfo rootx $wdName]+10]+[expr [winfo rooty $wdName]-30]
    message .balloon.text -text $label -bg yellow -relief solid -justify left \
        -aspect 800
    pack .balloon.text -fill both

    append wdSize [winfo reqwidth .balloon.text] x [winfo reqheight .balloon.text]
    wm geometry .balloon \
        $wdSize-[expr [winfo screenwidth .]-[winfo rootx $wdName]+10]+[expr [winfo rooty $wdName]+20]
    wm overrideredirect .balloon 1
}

proc ShowModules {} {
    global WDModules ThisTimer ThisModule DefPadX

    set title "Module Browser"

    if {[RegisterFormOrWindow $title modules WDModules ShowModules \
            "Done"          {} \
            ""              {} \
            "Show Timer"    {ShowTimers} \
            ""              {} \
        ]
    } {
        UpdateModule
        return
    }

    
    if {![info exists ThisModule]} {
        set ThisModule 0
    }

    if {$ThisModule==-1} {
        
    } else {
        set ThisTimer [goblin module $ThisModule timer]
    }

    destroy $WDModules.frame0

    frame $WDModules.frame0
    pack $WDModules.frame0 -side top -fill x -padx $DefPadX -pady 3m

    label $WDModules.frame0.timerText -text "Timer Unit: " -anchor w
    grid $WDModules.frame0.timerText \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m

    menubutton $WDModules.frame0.timerSelect \
        -menu $WDModules.frame0.timerSelect.menu -anchor w \
        -text [goblin timer $ThisTimer label] -bg white -relief ridge
    grid $WDModules.frame0.timerSelect \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m
    
    menu $WDModules.frame0.timerSelect.menu -tearoff false
    
    for {set i 0} {$i<[goblin #timers]} {incr i} {
        $WDModules.frame0.timerSelect.menu add command \
            -label [goblin timer $i label] \
            -command "set ThisTimer $i; set ThisModule -1; UpdateModule"
    }


    label $WDModules.frame0.codeText -text "Code Unit: " -anchor w
    grid $WDModules.frame0.codeText \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m

    menubutton $WDModules.frame0.codeSelect \
        -menu $WDModules.frame0.codeSelect.menu -anchor w \
        -text [goblin module $ThisModule name] -bg white -relief ridge
    grid $WDModules.frame0.codeSelect \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m
    
    menu $WDModules.frame0.codeSelect.menu -tearoff false
    
    for {set i 0} {$i<[goblin #modules]} {incr i} {
        if {[goblin module $i timer]!=$ThisTimer} {continue}

        $WDModules.frame0.codeSelect.menu add command \
            -label [goblin module $i name] \
            -command "set ThisModule $i; UpdateModule"
    }

    grid columnconfig $WDModules.frame0 0 -weight 0 -minsize 1
    grid columnconfig $WDModules.frame0 1 -weight 1 -minsize 0


    destroy $WDModules.frame2

    frame $WDModules.frame2
    pack $WDModules.frame2 -side top -fill x -padx $DefPadX -pady 3m


    destroy $WDModules.frame1

    frame $WDModules.frame1
    pack $WDModules.frame1 -side top -fill x -padx $DefPadX -pady 3m

    label $WDModules.frame1.encodingText -text "Implementation Date: " -anchor w
    grid $WDModules.frame1.encodingText \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    label $WDModules.frame1.encodingValue -anchor w -relief ridge
    grid $WDModules.frame1.encodingValue \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m

    label $WDModules.frame1.revisionText -text "Latest Revision: " -anchor w
    grid $WDModules.frame1.revisionText \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    label $WDModules.frame1.revisionValue -anchor w -relief ridge
    grid $WDModules.frame1.revisionValue \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m

    frame $WDModules.frame1.separator -height 3m
    grid $WDModules.frame1.separator \
        -row 2 -column 0 -rowspan 1 -columnspan 2

    label $WDModules.frame1.originalText -text "Original Reference: " -anchor w
    grid $WDModules.frame1.originalText \
        -row 3 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    label $WDModules.frame1.originalValue -anchor w -relief ridge
    grid $WDModules.frame1.originalValue \
        -row 3 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m

    label $WDModules.frame1.authRefText -text "Authors Reference: " -anchor w
    grid $WDModules.frame1.authRefText \
        -row 4 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    label $WDModules.frame1.authRefValue -anchor w -relief ridge
    grid $WDModules.frame1.authRefValue \
        -row 4 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m

    label $WDModules.frame1.bookText -text "Text Book: " -anchor w
    grid $WDModules.frame1.bookText \
        -row 5 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    label $WDModules.frame1.bookValue -anchor w -relief ridge
    grid $WDModules.frame1.bookValue \
        -row 5 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m

    grid columnconfig $WDModules.frame1 0 -weight 0 -minsize 1
    grid columnconfig $WDModules.frame1 1 -weight 1 -minsize 0
    
    bind $WDModules.frame0.codeSelect <Prior> {
        if {$ThisModule==0} {
            set ThisModule [expr [goblin #modules]-1]
        } else {
            incr ThisModule -1
        }
        
        UpdateModule
    }
    
    bind $WDModules.frame0.codeSelect <Next> {
        if {$ThisModule==[expr [goblin #modules]-1]} {
            set ThisModule 0
        } else {
            incr ThisModule
        }
        
        UpdateModule
    }


    UpdateModule

    DisplayForm $title
}


proc UpdateModule {} {
    global WDModules ThisTimer ThisModule

    if {$ThisModule==-1} {
        # Timer unit has changed
    
        $WDModules.frame0.timerSelect configure \
            -text [goblin timer $ThisTimer label]

        destroy  $WDModules.frame0.codeSelect.menu
        menu $WDModules.frame0.codeSelect.menu -tearoff false

        for {set i 0} {$i<[goblin #modules]} {incr i} {
            if {[goblin module $i timer]!=$ThisTimer} {continue}

            if {$ThisModule==-1} {set ThisModule $i}
            
            $WDModules.frame0.codeSelect.menu add command \
                -label [goblin module $i name] \
                -command "set ThisModule $i; UpdateModule"
        }
    } else {
        # Code unit has changed
    }

    $WDModules.frame0.timerSelect configure \
        -text [goblin timer [goblin module $ThisModule timer] label]

    $WDModules.frame0.codeSelect configure \
        -text [goblin module $ThisModule name]


    destroy  $WDModules.frame2.impl1Text
    destroy  $WDModules.frame2.impl1Name
    destroy  $WDModules.frame2.impl2Text
    destroy  $WDModules.frame2.impl2Name

    label $WDModules.frame2.impl1Text -text "Implementor: " -anchor w
    grid $WDModules.frame2.impl1Text \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
    label $WDModules.frame2.impl1Name -anchor w -relief ridge
    grid $WDModules.frame2.impl1Name \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m

    set tmpRef [goblin module $ThisModule implementor1]
    if {$tmpRef!=[goblin #authors]} {
        $WDModules.frame2.impl1Name configure -text \
            [goblin author $tmpRef name]
        bind $WDModules.frame2.impl1Name <Double-1> \
            "set ThisAuthor $tmpRef; ShowAuthors"
    } else {
        $WDModules.frame2.impl1Name configure -text "Unknown"
        bind $WDModules.frame2.impl1Name <Double-1> {}
    }

    set tmpRef [goblin module $ThisModule implementor2]
    if {$tmpRef!=[goblin #authors]} {
        $WDModules.frame2.impl1Text configure -text "1st Implementor: "
        label $WDModules.frame2.impl2Text -text "2nd Implementor: " -anchor w
        grid $WDModules.frame2.impl2Text \
            -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky w -padx 1m
        label $WDModules.frame2.impl2Name -anchor w -relief ridge -text \
            [goblin author $tmpRef name]
        grid $WDModules.frame2.impl2Name \
            -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky we -padx 1m
        bind $WDModules.frame2.impl2Name <Double-1> \
            "set ThisAuthor $tmpRef; ShowAuthors"
    }

    grid columnconfig $WDModules.frame2 0 -weight 0 -minsize 1
    grid columnconfig $WDModules.frame2 1 -weight 1 -minsize 0
    

    set tmpRef [goblin module $ThisModule encoding_date]
    if {$tmpRef==""} {
        $WDModules.frame1.encodingValue configure -text "Unknown"
    } else {
        $WDModules.frame1.encodingValue configure -text $tmpRef
    }
    
    set tmpRef [goblin module $ThisModule revision_date]
    if {$tmpRef==""} {
        $WDModules.frame1.revisionValue configure -text "Unknown"
    } else {
        $WDModules.frame1.revisionValue configure -text $tmpRef
    }
    
    set tmpRef [goblin module $ThisModule original_reference]
    if {$tmpRef==[goblin #references]} {
        $WDModules.frame1.originalValue configure -text "Unknown"
        bind $WDModules.frame1.originalValue <Enter> {}
        bind $WDModules.frame1.originalValue <Leave> {}
        bind $WDModules.frame1.originalValue <Double-1> {}
    } else {
        $WDModules.frame1.originalValue configure -text \
            [goblin reference $tmpRef key]
        bind $WDModules.frame1.originalValue <Enter> \
            [list MakeBalloonModules \
                "[goblin reference $tmpRef authors]: [goblin reference $tmpRef title]" \
                $WDModules.frame1.originalValue]
        bind $WDModules.frame1.originalValue <Leave> {DestroyBalloon}
        bind $WDModules.frame1.originalValue <Double-1> \
            "set ThisReference $tmpRef; DestroyBalloon; ShowBibliography"
    }
    
    set tmpRef [goblin module $ThisModule authors_reference]
    if {$tmpRef==[goblin #references]} {
        $WDModules.frame1.authRefValue configure -text "Unknown"
        bind $WDModules.frame1.authRefValue <Enter> {}
        bind $WDModules.frame1.authRefValue <Leave> {}
        bind $WDModules.frame1.authRefValue <Double-1> {}
    } else {
        $WDModules.frame1.authRefValue configure -text \
            [goblin reference $tmpRef key]
        bind $WDModules.frame1.authRefValue <Enter> \
            [list MakeBalloonModules \
                "[goblin reference $tmpRef authors]: [goblin reference $tmpRef title]" \
                $WDModules.frame1.authRefValue]
        bind $WDModules.frame1.authRefValue <Leave> {DestroyBalloon}
        bind $WDModules.frame1.authRefValue <Double-1> \
            "set ThisReference $tmpRef; DestroyBalloon; ShowBibliography"
    }
    
    set tmpRef [goblin module $ThisModule text_book]
    if {$tmpRef==[goblin #references]} {
        $WDModules.frame1.bookValue configure -text "Unknown"
        bind $WDModules.frame1.bookValue <Enter> {}
        bind $WDModules.frame1.bookValue <Leave> {}
        bind $WDModules.frame1.bookValue <Double-1> {}
    } else {
        $WDModules.frame1.bookValue configure -text \
            [goblin reference $tmpRef key]
        bind $WDModules.frame1.bookValue <Enter> \
            [list MakeBalloonModules \
                "[goblin reference $tmpRef authors]: [goblin reference $tmpRef title]" \
                $WDModules.frame1.bookValue]
        bind $WDModules.frame1.bookValue <Leave> {DestroyBalloon}
        bind $WDModules.frame1.bookValue <Double-1> \
            "set ThisReference $tmpRef; DestroyBalloon; ShowBibliography"
    }

    focus $WDModules.frame0.codeSelect

    AlignActiveDialog
}

ShowModules

