#!/usr/local/tk/bin/wish -f

# file: channel_handler.tcl
#

# This is a small application that allows the status of n channels to
# be monitored 
#

global filename
global num_chans
global free_string
global p
global chan_stat
global edit_flag cont_flag

set cont_flag 1

set proc_filename "$env(LINKON)/proc/channels.dat"
set num_chans 12
set free_string "ffffffffffff"
set p ".channel_proc"
set edit_flag 1

proc update_data {} {
    global proc_filename
    global p
    global chan_stat

    set fp [open $proc_filename]
    gets $fp line
    set chan_stat $line
    close $fp

}

proc change_data {change_string} {
    global proc_filename

    set fp [open $proc_filename w]
    puts $fp $change_string
    close $fp
    update_data
}    

toplevel $p

wm withdraw "."

bind $p <Enter> {
    set edit_flag 0
    update_data
}
bind $p <Leave> {
    set edit_flag 1
    update_data
}

entry $p.channel_dat -width $num_chans -textvariable chan_stat -borderwidth 3

# button $p.free -text "free" -command {change_data $free_string} -width 3

wm protocol $p WM_DELETE_WINDOW {
    set cont_flag 0
    destroy $p
    exit
}

bind $p.channel_dat <Button-1> {change_data $chan_stat}

bind $p.channel_dat <Return> {change_data $chan_stat}

bind $p.channel_dat <Button-3> {change_data $free_string}

pack $p.channel_dat -side left

update_data

proc foo {} {
    global edit_flag
    after 5 {
	if {$edit_flag == 1} {
	    update_data
	}
	update
	if {$cont_flag == 1} {
	    foo
	}
    }
}

foo

# end of file
#
