
set ::auto_path [linsert $::auto_path 0 .]

package require Tk
package require Canvas3d

canvas3d .win -width 600 -height 400 -background black
frame .t
scrollbar .t.scroll -command {.t.text yview}
text .t.text -height 10 -yscrollcommand {.t.scroll set}
button .button -text Continue -command i_continue
button .toggle -text {Toggle Description/Script} -command i_toggle

pack .win    -expand yes -fill both

pack .t -expand yes -fill x
pack .t.text   -side left -expand yes -fill x
pack .t.scroll -side right -expand yes -fill y

pack .button -side right -expand yes -fill x
pack .toggle

set ::DB [list]
set ::I -1
set ::DISPLAY_SCRIPT 0

# Set the contents of the text widget.
proc i_text {text} {
    .t.text delete 0.0 end
    .t.text insert 0.0 [string trim $text]
}

proc i_test {testname description script} {
    lappend ::DB [list $description $script $testname]
}

proc i_toggle {} {
    set ::DISPLAY_SCRIPT [expr ($::DISPLAY_SCRIPT + 1) % 2]
    i_text [lindex $::DB $::I $::DISPLAY_SCRIPT]
}

proc i_exit {} {
    destroy .win
    destroy .button
    destroy .toggle
    destroy .t.text
    destroy .t.scroll
    destroy .t
    array set allocs [canvas3d_alloc]
    foreach key [array names allocs] {
        if {$allocs($key) != 0} {
            puts stderr "ERROR: $n unmatched C3dAlloc() calls in library"
        }
    }
    exit
}

proc i_continue {} {
    incr ::I
    if {$::I == [llength $::DB]} i_exit
    i_text [lindex $::DB $::I $::DISPLAY_SCRIPT]
    eval [lindex $::DB $::I 1]
    wm title . [lindex $::DB $::I 2]
}
after idle i_continue

if {[file tail $argv0] == [file tail [info script]]} {
    set dir [file dirname [info script]]
    foreach file [glob -directory $dir *.itest] {
        source $file
    }
}
