1#!/bin/sh
2# the next line restarts using wish \
3exec wish "$0" "$@"
4
5# gpdemos.tcl --
6#
7# Demonstration of gnuplot demos under Tcl/Tk.  Adapted from original
8# demo by D.M.Burns and image demo from Tcl/Tk.  To run under unix:
9#
10#    unix> wish gpdemos.tcl
11#
12# or if gpdemos.tcl is changed to executable:
13#
14#    unix> gpdemos.tcl
15#
16# Starting directory for demos is taken from environmental variable
17# GNUPLOT_LIB.
18#
19#  9 Sep 2004 - Original demo, Donald M. Burns
20# 12 Sep 2004 - Enhanced to search directory for *.dem files.
21
22# TAILOR THIS PATH IF NECESSARY
23set gnuplot    gnuplot
24
25if {[info exists env(GNUPLOT_LIB)]} {
26        set gp_library $env(GNUPLOT_LIB)
27} else {
28        # the environment var isn't set, default to current directory
29        set gp_library .
30}
31
32set demopath $gp_library
33
34eval destroy [winfo child .]
35
36wm protocol . WM_DELETE_WINDOW safe_exit
37
38# Open up a pipe to gnuplot
39set gpfd [open "|$gnuplot" w+]
40fconfigure $gpfd -buffering none -blocking 0
41
42# A procedure to give commands to gnuplot
43proc gnuplot {a} {
44    global gpfd
45
46    fileevent $gpfd writable [puts $gpfd $a]
47}
48
49# A procedure to clean-up on exit - this is broken for windows!
50proc safe_exit {} {
51    global gpfd
52
53    set pids [pid $gpfd]
54    foreach pid $pids {
55	exec kill $pid
56    }
57    close $gpfd
58    exit
59}
60
61# loadDir --
62# This procedure reloads the directory listbox from the contents
63# of the directory named in the demo's entry.
64proc loadDir {} {
65    global dirName
66
67    .sel.f.list delete 0 end
68    foreach i [lsort [glob -nocomplain [file join $dirName *.dem]]] {
69	.sel.f.list insert end [file tail $i]
70    }
71    foreach i [lsort [glob -nocomplain [file join $dirName *.gp]]] {
72	.sel.f.list insert end [file tail $i]
73    }
74}
75
76# loadImage --
77# Given the name of the toplevel window of the demo and the mouse
78# position, extracts the directory entry under the mouse and loads
79# that file into a photo image for display.
80#
81# Arguments:
82# x, y-			Mouse position within the listbox.
83proc loadDemo {x y} {
84    global dirName
85    global gpfd
86
87    # Send an ctrl-C to gnuplot in case a different demo is running.
88    exec kill -INT [pid $gpfd]
89
90    set file [file join $dirName [.sel.f.list get @$x,$y]]
91    puts stderr "Loading demo $file"
92
93    # Some demos may have set terminal and failed to set it back.
94    gnuplot "\nset term x11; reset; load \"$file\""
95}
96
97set font {Helvetica 11}
98
99wm title . "Tcl/Tk Gnuplot Demonstration"
100wm iconname . "Tcl/Tk GP"
101
102frame .sel
103frame .sep -relief ridge -bd 1 -width 2
104frame .plt
105pack .sel -side left -fill y -padx 10 -pady 10 -expand no
106pack .sep -side left -fill y -expand no
107pack .plt -side right -fill both -expand yes -padx 10 -pady 10
108
109label .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."
110pack .sel.msg -side top
111
112## Create selection side of window
113frame .sel.buttons
114pack .sel.buttons -side bottom -fill both -expand no
115button .sel.buttons.quit -text Quit -command "safe_exit"
116pack .sel.buttons.quit -side left -expand 1 -fill both
117#
118label .sel.dirLabel -text "Directory:"
119set dirName [file join $gp_library]
120entry .sel.dirName -textvariable dirName
121bind .sel.dirName <Return> "loadDir"
122frame .sel.spacer1 -height 3m
123label .sel.fileLabel -text "File:"
124frame .sel.f
125frame .sel.spacer2 -height 2m
126pack .sel.dirLabel -side top -anchor w
127pack .sel.dirName -side top -fill x
128pack .sel.spacer1 .sel.fileLabel -side top -anchor w
129pack .sel.f -fill both -expand 1
130pack .sel.spacer2 -side top
131#
132listbox .sel.f.list -yscrollcommand ".sel.f.scroll set"
133scrollbar .sel.f.scroll -command ".sel.f.list yview"
134pack .sel.f.list -side left -fill both -expand 1
135pack .sel.f.scroll -side right -fill y -expand 0
136bind .sel.f.list <Double-1> "loadDemo %x %y"
137
138## Run load directory routine to fill list box
139loadDir
140
141## Create the plot side of window
142frame .plt.g -bg "" -width 640 -height 450
143frame .plt.spacer2 -height 2m
144frame .plt.buttons
145button .plt.buttons.bnext -text Next -command {gnuplot ""}
146button .plt.buttons.bstop -text Reset -command {exec kill -INT [pid $gpfd]; gnuplot "\nreset; clear"}
147#
148pack .plt.buttons.bnext .plt.buttons.bstop -side left -expand 1
149pack .plt.buttons -side bottom -fill both -expand no
150pack .plt.spacer2 -side bottom
151pack .plt.g -side top -expand true -fill both
152
153# Tell gnuplot to use our frame .plt.g for display
154gnuplot "set term x11 window '[winfo id .plt.g]'"
155gnuplot "clear"
156