# Demonstration of gnuplot demos under Tcl/Tk. Adapted from original
# demo by D.M.Burns and image demo from Tcl/Tk. To run under unix:
#
# unix> wish gpdemos.tcl
#
# or if gpdemos.tcl is changed to executable:
#
# unix> gpdemos.tcl
#
# Starting directory for demos is taken from environmental variable
# GNUPLOT_LIB.
#
# 9 Sep 2004 - Original demo, Donald M. Burns
# 12 Sep 2004 - Enhanced to search directory for *.dem files.
# TAILOR THIS PATH IF NECESSARY
set gnuplot gnuplot
if {[info exists env(GNUPLOT_LIB)]} {
set gp_library $env(GNUPLOT_LIB)
} else {
# the environment var isn't set, default to current directory
set gp_library .
}
set demopath $gp_library
eval destroy [winfo child .]
wm protocol . WM_DELETE_WINDOW safe_exit
# Open up a pipe to gnuplot
set gpfd [open"|$gnuplot" w+]
fconfigure$gpfd -buffering none -blocking 0
# A procedure to give commands to gnuplot
procgnuplot {a} {
global gpfd
fileevent$gpfd writable [puts$gpfd$a]
}
# A procedure to clean-up on exit - this is broken for windows!
procsafe_exit {} {
global gpfd
set pids [pid$gpfd]
foreach pid $pids {
exec kill $pid
}
close$gpfd
exit
}
# loadDir --
# This procedure reloads the directory listbox from the contents
# of the directory named in the demo's entry.
procloadDir {} {
global dirName
.sel.f.list delete 0 end
foreach i [lsort [glob -nocomplain [file join $dirName *.dem]]] {
.sel.f.list insert end [file tail $i]
}
foreach i [lsort [glob -nocomplain [file join $dirName *.gp]]] {
.sel.f.list insert end [file tail $i]
}
}
# loadImage --
# Given the name of the toplevel window of the demo and the mouse
# position, extracts the directory entry under the mouse and loads
# that file into a photo image for display.
#
# Arguments:
# x, y- Mouse position within the listbox.
procloadDemo {x y} {
global dirName
global gpfd
# Send an ctrl-C to gnuplot in case a different demo is running.
exec kill -INT [pid$gpfd]
set file [file join $dirName [.sel.f.list get @$x,$y]]
puts stderr "Loading demo $file"
# Some demos may have set terminal and failed to set it back.
gnuplot "\nset term x11; reset; load \"$file\""
}
set font {Helvetica 11}
wm title . "Tcl/Tk Gnuplot Demonstration"
wm iconname . "Tcl/Tk GP"
frame .sel
frame .sep -relief ridge -bd 1 -width 2
frame .plt
pack .sel -side left -fill y -padx 10 -pady 10 -expand no
pack .sep -side left -fill y -expand no
pack .plt -side right -fill both -expand yes -padx 10 -pady 10
label .sel.msg -font $font -wraplength 150 -justify left -text "This is an example of using gnuplot to draw into an X window opened by an external application. It allows you to run the demo listed demo scripts individually."
pack .sel.msg -side top
## Create selection side of window
frame .sel.buttons
pack .sel.buttons -side bottom -fill both -expand no