
#-------------------------------------------------------------------------------
# Module:     infoTimers.tk
# Activation: Dynamically loaded on user request
# Scope:      Access to timers
#-------------------------------------------------------------------------------



proc ShowTimers {} {
    global WDTimers InternalWindowing DialogFrame ThisTimer DefPadX

    if {$InternalWindowing} {
        set WDTimers $DialogFrame.timers
        if {![winfo exists $WDTimers]} {
            frame $WDTimers
        }
        
        RegisterDialog "Timer Info" $WDTimers ShowTimers {}
    } else {
        set WDTimers .timers
        if {![winfo exists $WDTimers]} {
            toplevel $WDTimers
            wm title $WDTimers "Timer Info"
            wm resizable $WDTimers 0 0
        } else {return}
    }
    
    if {![info exists ThisTimer]} {set ThisTimer 0}

    destroy $WDTimers.frame0

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

    label $WDTimers.frame0.text -text "Timer: " -anchor w
    pack $WDTimers.frame0.text -side left

    menubutton  $WDTimers.frame0.select -menu $WDTimers.frame0.select.menu \
        -text [goblin timer $ThisTimer label] -bg white -relief ridge
    pack $WDTimers.frame0.select -side left -fill x -expand 1
    
    menu $WDTimers.frame0.select.menu
    
    for {set i 0} {$i<[goblin #timers]} {incr i} {
        $WDTimers.frame0.select.menu add command \
            -label [goblin timer $i label] \
            -command "set ThisTimer $i; UpdateTimer"
    }


    destroy $WDTimers.frame1

    frame $WDTimers.frame1
    pack $WDTimers.frame1 -side top -fill x -padx $DefPadX -pady 5m

    label $WDTimers.frame1.accText -text "Cumulated Times: " -anchor w
    grid $WDTimers.frame1.accText \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.accValue -text "[goblin timer $ThisTimer acc]" \
        -anchor e
    grid $WDTimers.frame1.accValue \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.accUnit -text "ms" -anchor w
    grid $WDTimers.frame1.accUnit \
        -row 0 -column 2 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $WDTimers.frame1.prevText -text "Previous Round: " -anchor w
    grid $WDTimers.frame1.prevText \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.prevValue -text "[goblin timer $ThisTimer prev]" \
        -anchor e
    grid $WDTimers.frame1.prevValue \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.prevUnit -text "ms" -anchor w
    grid $WDTimers.frame1.prevUnit \
        -row 1 -column 2 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $WDTimers.frame1.minText -text "Minimum Time: " -anchor w
    grid $WDTimers.frame1.minText \
        -row 2 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.minValue -text "[goblin timer $ThisTimer min]" \
        -anchor e
    grid $WDTimers.frame1.minValue \
        -row 2 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.minUnit -text "ms" -anchor w
    grid $WDTimers.frame1.minUnit \
        -row 2 -column 2 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $WDTimers.frame1.avgText -text "Average Time: " -anchor w
    grid $WDTimers.frame1.avgText \
        -row 3 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.avgValue -text "[goblin timer $ThisTimer av]" \
        -anchor e
    grid $WDTimers.frame1.avgValue \
        -row 3 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.avgUnit -text "ms" -anchor w
    grid $WDTimers.frame1.avgUnit \
        -row 3 -column 2 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    label $WDTimers.frame1.maxText -text "Maximum Time: " -anchor w
    grid $WDTimers.frame1.maxText \
        -row 4 -column 0 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.maxValue -text "[goblin timer $ThisTimer max]" \
        -anchor e
    grid $WDTimers.frame1.maxValue \
        -row 4 -column 1 -rowspan 1 -columnspan 1 -sticky news -padx 1m
    label $WDTimers.frame1.maxUnit -text "ms" -anchor w
    grid $WDTimers.frame1.maxUnit \
        -row 4 -column 2 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    grid rowconfig    $WDTimers.frame1 0 -weight 1 -minsize 0
    grid columnconfig $WDTimers.frame1 0 -weight 1 -minsize 0


    destroy $WDTimers.frame2

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

    label $WDTimers.frame2.label1 -text "Dependent Timer" -anchor w
    grid $WDTimers.frame2.label1 -in $WDTimers.frame2 \
        -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news

    label $WDTimers.frame2.label2 -text "ms" -anchor w
    grid $WDTimers.frame2.label2 -in $WDTimers.frame2 \
        -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news

    label $WDTimers.frame2.label3 -text "%" -anchor w
    grid $WDTimers.frame2.label3 -in $WDTimers.frame2 \
        -row 0 -column 2 -rowspan 1 -columnspan 1 -sticky news

    listbox $WDTimers.frame2.list1 -yscrollcommand "$WDTimers.frame2.scroll set" \
        -width 15 -height 3
    grid $WDTimers.frame2.list1 -in $WDTimers.frame2 \
        -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news

    listbox $WDTimers.frame2.list2 -yscrollcommand "$WDTimers.frame2.scroll set" \
        -width 6 -height 3
    grid $WDTimers.frame2.list2 -in $WDTimers.frame2 \
        -row 1 -column 1 -rowspan 1 -columnspan 1 -sticky news

    listbox $WDTimers.frame2.list3 -yscrollcommand "$WDTimers.frame2.scroll set" \
        -width 4 -height 3
    grid $WDTimers.frame2.list3 -in $WDTimers.frame2 \
        -row 1 -column 2 -rowspan 1 -columnspan 1 -sticky news

    scrollbar $WDTimers.frame2.scroll -width 3.5m -command BindTimerView
    grid $WDTimers.frame2.scroll -in $WDTimers.frame2 \
        -row 1 -column 3 -rowspan 1 -columnspan 1 -sticky news -padx 1m

    grid rowconfig    $WDTimers.frame2 0 -weight 1 -minsize 0
    grid columnconfig $WDTimers.frame2 0 -weight 1 -minsize 0

    bind $WDTimers.frame2.list1 <Double-Button-1> {
        global TimsTimer
        set thisLabel [selection get]

        for {set i 0} {$i<[goblin #timers] && \
            $thisLabel!=[goblin timer $i label]} {incr i} {}
        if {$i<[goblin #timers]} {
            set ThisTimer $i
            UpdateTimer
        }
    }
    
    frame $WDTimers.buttons
    button $WDTimers.buttons.update -text "Update" -command { \
        UpdateTimer
    }

    button $WDTimers.buttons.reset -text "Reset" -command { \
        goblin timer $ThisTimer reset
        UpdateTimer
    }

    button $WDTimers.buttons.done -text "Done" -command { \
        if {$InternalWindowing} {
            DestroyActiveDialog
        } else {
            destroy $WDTimers
        }
    }

    pack $WDTimers.buttons.update $WDTimers.buttons.reset \
        $WDTimers.buttons.done -side left -fill x -padx 3m

    pack $WDTimers.buttons -padx 3m -pady 3m -side bottom


    UpdateTimer


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


proc UpdateTimer {} {
    global ThisGraph WDTimers InternalWindowing DialogFrame \
        DefPadX DefPadY ThisTimer

    $WDTimers.frame0.select configure -text [goblin timer $ThisTimer label]

    $WDTimers.frame1.accValue configure -text "[goblin timer $ThisTimer acc]"
    $WDTimers.frame1.prevValue configure -text "[goblin timer $ThisTimer prev]"
    $WDTimers.frame1.minValue configure -text "[goblin timer $ThisTimer min]"
    $WDTimers.frame1.avgValue configure -text "[goblin timer $ThisTimer av]"
    $WDTimers.frame1.maxValue configure -text "[goblin timer $ThisTimer max]"
    
    $WDTimers.frame2.list1 delete 0 end
    $WDTimers.frame2.list2 delete 0 end
    $WDTimers.frame2.list3 delete 0 end
    set totalTime [goblin timer $ThisTimer prev]
    for {set i 0} {$i<[goblin #timers]} {incr i} {
        set thisTime [goblin timer $ThisTimer child $i]
        
        if {$thisTime>0 && $i!=$ThisTimer} {
            set relativeTime [expr round($thisTime/($totalTime+0.0001)*1000)*0.1]
            $WDTimers.frame2.list1 insert end "[goblin timer $i label]"
            $WDTimers.frame2.list2 insert end "$thisTime"
            $WDTimers.frame2.list3 insert end "$relativeTime"
        }
    }
}


proc BindTimerView {args} {
    global WDTimers

    eval {$WDTimers.frame2.list1 yview} $args
    eval {$WDTimers.frame2.list2 yview} $args
    eval {$WDTimers.frame2.list3 yview} $args
}


if {![info exists WDTimers] || ![winfo exists $WDTimers]} {ShowTimers}

