
# Code for tracing events.

proc trace_events_start {file} {
    global trace_events
    set trace_events(file) $file
    set trace_events(fp) [open $file w]
    
    bind all <ButtonPress> "trace_events ButtonPress W %W x %x y %x b %b s %s"
    bind all <ButtonRelease> "trace_events ButtonRelease W %W x %x y %x b %b s %s"
    bind all <KeyPress> "trace_events KeyPress W %W x %x y %x K %K"
    bind all <KeyRelease> "trace_events KeyRelease W %W x %x y %x K %K"
}

proc trace_events_end {} {
    global trace_events
    close $trace_events(fp)
    unset trace_events(fp)
}

proc trace_events {type args} {
    global trace_events
    if [info exists trace_events(fp)] {
	set args2 {}
	foreach {n v} $args {
	    switch $n {
		W {
		    set class [winfo class $v]
		    lappend args2 $n $v
		}
		default {
		    lappend args2 $n [list $v]
		}
	    }
	}
	puts $trace_events(fp) [concat [list $type $class] $args2]
    }
}
