Mercurial > hg > octave-lyh
annotate scripts/plot/gnuplot_drawnow.m @ 8249:1f429086565c
[mq]: hidden-children
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 21 Oct 2008 14:06:25 -0400 |
parents | a0ec02774303 |
children | d750feaefa8e |
rev | line source |
---|---|
7408 | 1 ## Copyright (C) 2005, 2006, 2007 John W. Eaton |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} drawnow () | |
21 ## Update and display the current graphics. | |
22 ## | |
23 ## Octave automatically calls drawnow just before printing a prompt, | |
24 ## when @code{sleep} or @code{pause} is called, or while waiting for | |
25 ## command-line input. | |
26 ## @end deftypefn | |
27 | |
28 ## Author: jwe | |
29 | |
30 function gnuplot_drawnow (h, term, file, mono, debug_file) | |
31 | |
32 if (nargin < 4) | |
33 mono = false; | |
34 endif | |
35 | |
36 if (nargin >= 3 && nargin <= 5) | |
37 plot_stream = []; | |
38 fid = []; | |
39 unwind_protect | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
40 [plot_stream, enhanced] = open_gnuplot_stream (1, [], term, file); |
8249 | 41 __go_draw_figure__ (h, plot_stream, enhanced, mono); |
7408 | 42 if (nargin == 5) |
43 fid = fopen (debug_file, "wb"); | |
44 enhanced = init_plot_stream (fid, [], term, file); | |
8249 | 45 __go_draw_figure__ (h, fid, enhanced, mono); |
7408 | 46 endif |
47 unwind_protect_cleanup | |
48 if (! isempty (plot_stream)) | |
49 pclose (plot_stream); | |
50 endif | |
51 if (! isempty (fid)) | |
52 fclose (fid); | |
53 endif | |
54 end_unwind_protect | |
55 elseif (nargin == 1) | |
8249 | 56 plot_stream = get (h, "__plot_stream__"); |
7408 | 57 if (isempty (plot_stream)) |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
58 [plot_stream, enhanced] = open_gnuplot_stream (2, h); |
7408 | 59 set (h, "__enhanced__", enhanced); |
60 else | |
8249 | 61 enhanced = get (h, "__enhanced__"); |
7408 | 62 endif |
8249 | 63 __go_draw_figure__ (h, plot_stream (1), enhanced, mono); |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
64 fflush (plot_stream (1)); |
7408 | 65 else |
66 print_usage (); | |
67 endif | |
68 | |
69 endfunction | |
70 | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
71 function [plot_stream, enhanced] = open_gnuplot_stream (npipes, h, varargin) |
7408 | 72 |
73 cmd = gnuplot_binary (); | |
74 | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
75 if (npipes > 1) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
76 [plot_stream(1), plot_stream(2), pid] = popen2 (cmd); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
77 if (pid < 0) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
78 error ("drawnow: failed to open connection to gnuplot"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
79 endif |
7408 | 80 else |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
81 plot_stream = popen (cmd, "w"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
82 if (plot_stream < 0) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
83 error ("drawnow: failed to open connection to gnuplot"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
84 endif |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
85 endif |
7408 | 86 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
87 if (! isempty (h)) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
88 set (h, "__plot_stream__", plot_stream); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
89 endif |
7408 | 90 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
91 enhanced = init_plot_stream (plot_stream (1), h, varargin{:}); |
7408 | 92 |
93 endfunction | |
94 | |
95 function enhanced = init_plot_stream (plot_stream, h, term, file) | |
96 | |
97 if (nargin == 4) | |
98 enhanced = enhanced_term (term); | |
99 if (! isempty (term)) | |
100 if (enhanced) | |
101 fprintf (plot_stream, "set terminal %s enhanced;\n", term); | |
102 else | |
103 fprintf (plot_stream, "set terminal %s;\n", term); | |
104 endif | |
105 endif | |
106 if (! isempty (file)) | |
107 fprintf (plot_stream, "set output \"%s\";\n", file); | |
108 endif | |
109 else | |
110 | |
111 ## Guess the terminal type. | |
112 term = getenv ("GNUTERM"); | |
113 if (isempty (term)) | |
114 if (! isempty (getenv ("DISPLAY"))) | |
115 term = "x11"; | |
116 elseif (! isunix ()) | |
117 term = "windows"; | |
118 else | |
119 ## This should really be checking for os x before setting | |
120 ## the terminal type to aqua, but nobody will notice because | |
121 ## every other unix will be using x11 and windows will be | |
122 ## using windows. Those diehards still running octave from | |
123 ## a linux console know how to set the GNUTERM variable. | |
124 term = "aqua"; | |
125 endif | |
126 endif | |
127 | |
128 enhanced = enhanced_term (term); | |
129 if (enhanced) | |
130 enh_str = "enhanced"; | |
131 else | |
132 enh_str = ""; | |
133 endif | |
134 | |
135 ## If no 'h' (why not?) then open the terminal as Figure 0. | |
136 if (isempty (h)) | |
137 h = 0; | |
138 endif | |
139 | |
140 if (strcmp (term, "x11")) | |
141 fprintf (plot_stream, "set terminal x11 %s title \"Figure %d\"\n", | |
142 enh_str, h); | |
143 elseif (strcmp (term, "aqua")) | |
144 ## Aqua doesn't understand the 'title' option despite what the | |
145 ## gnuplot 4.2 documentation says. | |
146 fprintf (plot_stream, "set terminal aqua %d %s\n", h, enh_str); | |
147 elseif (strcmp (term, "wxt")) | |
148 fprintf (plot_stream, "set terminal wxt %s title \"Figure %d\"\n", | |
149 enh_str, h); | |
150 | |
151 elseif (enhanced) | |
152 fprintf (plot_stream, "set terminal %s %s\n", term, enh_str); | |
153 endif | |
154 ## gnuplot will pick up the GNUTERM environment variable itself | |
155 ## so no need to set the terminal type if not also setting the | |
156 ## figure title or enhanced mode. | |
157 | |
158 endif | |
159 | |
160 endfunction | |
161 | |
162 function have_enhanced = enhanced_term (term) | |
163 persistent enhanced_terminals; | |
164 | |
165 if (isempty (enhanced_terminals)) | |
166 ## Don't include pstex, pslatex or epslatex here as the TeX commands | |
167 ## should not be interpreted in that case. | |
168 if (compare_versions (__gnuplot_version__ (), "4.0", ">")) | |
169 enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ... | |
170 "windows", "wxt", "svg", "postscript", "x11", "pdf"}; | |
171 else | |
172 enhanced_terminals = {"x11", "postscript"}; | |
173 endif | |
174 endif | |
175 | |
176 term = tolower (term); | |
177 | |
178 have_enhanced = false; | |
179 for i = 1 : length (enhanced_terminals) | |
180 t = enhanced_terminals{i}; | |
181 if (strncmp (term, t, min (length (term), length (t)))) | |
182 have_enhanced = true; | |
183 break; | |
184 endif | |
185 endfor | |
186 endfunction |