File: gpdemos.tcl

package info (click to toggle)
gnuplot 6.0.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,940 kB
  • sloc: ansic: 95,319; cpp: 7,590; makefile: 2,470; javascript: 2,328; sh: 1,531; lisp: 664; perl: 304; pascal: 191; tcl: 88; python: 46
file content (155 lines) | stat: -rw-r--r-- 4,502 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

# gpdemos.tcl --
#
# 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
proc gnuplot {a} {
    global gpfd

    fileevent $gpfd writable [puts $gpfd $a]
}

# A procedure to clean-up on exit - this is broken for windows!
proc safe_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.
proc loadDir {} {
    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.
proc loadDemo {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
button .sel.buttons.quit -text Quit -command "safe_exit"
pack .sel.buttons.quit -side left -expand 1 -fill both
#
label .sel.dirLabel -text "Directory:"
set dirName [file join $gp_library]
entry .sel.dirName -textvariable dirName
bind .sel.dirName <Return> "loadDir"
frame .sel.spacer1 -height 3m
label .sel.fileLabel -text "File:"
frame .sel.f
frame .sel.spacer2 -height 2m
pack .sel.dirLabel -side top -anchor w
pack .sel.dirName -side top -fill x
pack .sel.spacer1 .sel.fileLabel -side top -anchor w
pack .sel.f -fill both -expand 1
pack .sel.spacer2 -side top
#
listbox .sel.f.list -yscrollcommand ".sel.f.scroll set"
scrollbar .sel.f.scroll -command ".sel.f.list yview"
pack .sel.f.list -side left -fill both -expand 1
pack .sel.f.scroll -side right -fill y -expand 0
bind .sel.f.list <Double-1> "loadDemo %x %y"

## Run load directory routine to fill list box
loadDir

## Create the plot side of window
frame .plt.g -bg "" -width 640 -height 450
frame .plt.spacer2 -height 2m
frame .plt.buttons
button .plt.buttons.bnext -text Next -command {gnuplot ""}
button .plt.buttons.bstop -text Reset -command {exec kill -INT [pid $gpfd]; gnuplot "\nreset; clear"}
#
pack .plt.buttons.bnext .plt.buttons.bstop -side left -expand 1
pack .plt.buttons -side bottom -fill both -expand no
pack .plt.spacer2 -side bottom
pack .plt.g -side top -expand true -fill both

# Tell gnuplot to use our frame .plt.g for display
gnuplot "set term x11 window '[winfo id .plt.g]'"
gnuplot "clear"