#!/usr/bin/env wish

# define three components to the window:

# text line with data value
# image region with image
# quit button

set picpath "/data/milo/cfh12k/plots"
set imname "(empty)"
set name "(empty)"

proc mk_layout { } {

    global file lasttime picpath

    set lasttime 0
    set file "$picpath/empty.gif"
    wm withdraw .

    set w [toplevel .t]
    wm title .t "user display"
    
    # top level containers
    frame $w.menubar -relief raised -borderwidth 2
    frame $w.picture -relief raised -borderwidth 2
    frame $w.dataline -relief raised -borderwidth 2

    pack $w.menubar -fill x
    pack $w.picture -fill x
    pack $w.dataline -fill x

    mk_menubar $w.menubar
    mk_picture $w.picture
    mk_dataline $w.dataline

    update
}

proc do_quit {} {
    
    exit 

}

proc mk_picture {w} {

    global file

    image create photo picture -file $file

    label $w.name -image picture
    pack  $w.name -side top -fill both
}

proc mk_menubar {w} {

    global file picpath

    menubutton $w.file -text "File" -menu $w.file.menu
    pack $w.file -side left

    menu $w.file.menu -tearoff 0
    $w.file.menu add command -label "Quit" -command { do_quit }
    bind all <Alt-q> { do_quit }
    
    menubutton $w.plot -text "Plots" -menu $w.plot.menu
    pack $w.plot -side left

    menu $w.plot.menu -tearoff 0
    $w.plot.menu add command -label "biases"          -command { set file "$picpath/biases.ppm"; refresh_now;  }
    $w.plot.menu add command -label "skyprobe recent" -command { set file "$picpath/skyprobe_recent.ppm"; refresh_now;  }
    $w.plot.menu add command -label "skyprobe night"  -command { set file "$picpath/skyprobe_night.ppm"; refresh_now;  }
    $w.plot.menu add command -label "sky vs time"     -command { set file "$picpath/sky.time.ppm"; refresh_now;  }
    $w.plot.menu add command -label "sky vs airmass"  -command { set file "$picpath/sky.airmass.ppm"; refresh_now;  }
    $w.plot.menu add command -label "FWHM vs time"    -command { set file "$picpath/fwhm.time.ppm"; refresh_now;  }
    $w.plot.menu add command -label "FWHM vs airmass" -command { set file "$picpath/fwhm.airmass.ppm"; refresh_now;  }
    $w.plot.menu add command -label "field (small)"   -command { set file "$picpath/field.small.ppm"; refresh_now;  }
    $w.plot.menu add command -label "field (large)"   -command { set file "$picpath/field.large.ppm"; refresh_now;  }
    $w.plot.menu add command -label "temperatures"    -command { set file "$picpath/temps.time.ppm"; refresh_now;  }
    $w.plot.menu add command -label "image (10x10)"   -command { set file "$picpath/current.b10.ppm"; refresh_now;  }
    $w.plot.menu add command -label "focus (unit)"    -command { set file "$picpath/focus.unit.ppm"; refresh_now;  }

}

proc mk_dataline {w} {

    global fwhm imname name
    set time [clock format [clock seconds] -format "%H:%M:%S"]

    label $w.label -text "name: $name  |  FHWM: 0.0  |  FILTER: X  |  time: $time            | image: $imname"
    pack $w.label -side left
    
}

proc do_refresh { } {

    global picpath imname name

    after 1000 {
	
	set time [clock format [clock seconds] -format "%H:%M:%S"]

	set name "empty"
	set fwhm 0.0
	set filter "X"

	if {! [catch "open $picpath/seeing.dat r" data] } {
	    gets $data line
	    close $data
	    set list [split $line]
	    set name [lindex $list 0]
	    set fwhm [format "%5.2f" [lindex $list 1]]
	    set filter [lindex $list 3]
	}

	.t.dataline.label configure -text "name: $name  |  FHWM: $fwhm  |  FILTER: $filter  |  time: $time           | image: $imname"
	update
	
	do_refresh 
    }
}

proc do_refresh_pic { } {

    global file lasttime imname name

    after 2000 {
	
	if {! [catch "file stat $file stats"] } {
	    set newtime $stats(mtime)
	    if {$newtime > $lasttime} {
		set tmpfile "$file.tmp"
		catch "exec ln -f $file $tmpfile"
		catch "picture read $tmpfile -shrink"
		catch "exec rm $tmpfile"
		set imname $name
		set lasttime $newtime
		update
	    }
	}
	do_refresh_pic
    }
}

proc refresh_now { } {

    global file lasttime imname name

    picture read $file -shrink;

    if {[file exists $file]} {
	set tmpfile "$file.tmp"
	catch "exec ln -f $file $tmpfile"
	catch "picture read $tmpfile -shrink"
	catch "exec rm -f $tmpfile"

	file stat $file stats
	set lasttime $stats(mtime)
	set imname $name
	
	update
	
    }
}

mk_layout
do_refresh
do_refresh_pic
