11740
|
1 ## Copyright (C) 2005, 2006, 2007, 2008 John W. Eaton |
6257
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6257
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6257
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} drawnow () |
6895
|
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. |
6257
|
26 ## @end deftypefn |
|
27 |
|
28 ## Author: jwe |
|
29 |
7269
|
30 function drawnow (term, file, mono, debug_file) |
6257
|
31 |
6454
|
32 persistent drawnow_executing = 0; |
|
33 |
|
34 unwind_protect |
|
35 |
|
36 ## If this is a recursive call, do nothing. |
|
37 if (++drawnow_executing > 1) |
|
38 return; |
6425
|
39 endif |
6454
|
40 |
7269
|
41 if (nargin < 3) |
|
42 mono = false; |
|
43 endif |
|
44 |
|
45 if (nargin >= 2 && nargin <= 4) |
11609
|
46 [dnm, fnm, ext] = fileparts (file); |
|
47 if (! (isempty (dnm) || isdir (dnm))) |
|
48 error ("drawnow: nonexistent directory `%s'", dnm); |
|
49 endif |
6454
|
50 h = get (0, "currentfigure"); |
|
51 if (h) |
6425
|
52 f = get (h); |
6454
|
53 plot_stream = []; |
6870
|
54 fid = []; |
6454
|
55 unwind_protect |
7189
|
56 [plot_stream, enhanced] = open_gnuplot_stream ([], term, file); |
7269
|
57 __go_draw_figure__ (f, plot_stream, enhanced, mono); |
|
58 if (nargin == 4) |
6870
|
59 fid = fopen (debug_file, "wb"); |
7189
|
60 enhanced = init_plot_stream (fid, [], term, file); |
7269
|
61 __go_draw_figure__ (f, fid, enhanced, mono); |
6870
|
62 endif |
6454
|
63 unwind_protect_cleanup |
|
64 if (! isempty (plot_stream)) |
6432
|
65 pclose (plot_stream); |
6425
|
66 endif |
6870
|
67 if (! isempty (fid)) |
|
68 fclose (fid); |
|
69 endif |
6454
|
70 end_unwind_protect |
|
71 else |
|
72 error ("drawnow: nothing to draw"); |
6425
|
73 endif |
6454
|
74 elseif (nargin == 0) |
|
75 for h = __go_figure_handles__ () |
|
76 if (! (isnan (h) || h == 0)) |
|
77 f = get (h); |
|
78 if (f.__modified__) |
|
79 plot_stream = f.__plot_stream__; |
6779
|
80 figure_is_visible = strcmp (f.visible, "on"); |
6454
|
81 if (figure_is_visible) |
|
82 if (isempty (plot_stream)) |
7189
|
83 [plot_stream, enhanced] = open_gnuplot_stream (h); |
|
84 set (h, "__enhanced__", enhanced); |
|
85 else |
|
86 enhanced = f.__enhanced__; |
6454
|
87 endif |
7269
|
88 __go_draw_figure__ (f, plot_stream, enhanced, mono); |
6454
|
89 elseif (! isempty (plot_stream)) |
|
90 pclose (plot_stream); |
|
91 set (h, "__plot_stream__", []); |
7189
|
92 set (h, "__enhanced__", false); |
6454
|
93 endif |
|
94 set (h, "__modified__", false); |
|
95 endif |
|
96 endif |
|
97 endfor |
|
98 else |
|
99 print_usage (); |
|
100 endif |
6425
|
101 |
6454
|
102 unwind_protect_cleanup |
|
103 |
|
104 drawnow_executing--; |
|
105 __request_drawnow__ (false); |
|
106 |
|
107 end_unwind_protect |
6425
|
108 |
|
109 endfunction |
|
110 |
7189
|
111 function [plot_stream, enhanced] = open_gnuplot_stream (h, varargin) |
6425
|
112 |
6418
|
113 ## If drawnow is cleared, it is possible to register __go_close_all__ |
|
114 ## more than once, but that is not fatal. |
|
115 persistent __go_close_all_registered__; |
6298
|
116 |
6425
|
117 cmd = gnuplot_binary (); |
6257
|
118 |
6425
|
119 plot_stream = popen (cmd, "w"); |
|
120 |
|
121 if (plot_stream < 0) |
|
122 error ("drawnow: failed to open connection to gnuplot"); |
|
123 else |
|
124 |
|
125 if (! isempty (h)) |
|
126 set (h, "__plot_stream__", plot_stream); |
6257
|
127 endif |
|
128 |
7189
|
129 enhanced = init_plot_stream (plot_stream, h, varargin{:}); |
6257
|
130 |
6425
|
131 if (isempty (__go_close_all_registered__)) |
|
132 atexit ("__go_close_all__"); |
|
133 __go_close_all_registered__ = true; |
6257
|
134 endif |
|
135 |
|
136 endif |
|
137 |
|
138 endfunction |
6870
|
139 |
7189
|
140 function enhanced = init_plot_stream (plot_stream, h, term, file) |
6870
|
141 |
|
142 if (nargin == 4) |
7189
|
143 enhanced = enhanced_term (term); |
6870
|
144 if (! isempty (term)) |
7189
|
145 if (enhanced) |
|
146 fprintf (plot_stream, "set terminal %s enhanced;\n", term); |
|
147 else |
|
148 fprintf (plot_stream, "set terminal %s;\n", term); |
|
149 endif |
6870
|
150 endif |
|
151 if (! isempty (file)) |
|
152 fprintf (plot_stream, "set output \"%s\";\n", file); |
|
153 endif |
|
154 else |
|
155 |
|
156 ## Guess the terminal type. |
|
157 term = getenv ("GNUTERM"); |
|
158 if (isempty (term)) |
|
159 if (! isempty (getenv ("DISPLAY"))) |
|
160 term = "x11"; |
|
161 elseif (! isunix ()) |
|
162 term = "windows"; |
|
163 else |
|
164 ## This should really be checking for os x before setting |
|
165 ## the terminal type to aqua, but nobody will notice because |
|
166 ## every other unix will be using x11 and windows will be |
|
167 ## using windows. Those diehards still running octave from |
|
168 ## a linux console know how to set the GNUTERM variable. |
|
169 term = "aqua"; |
|
170 endif |
|
171 endif |
|
172 |
7189
|
173 enhanced = enhanced_term (term); |
|
174 if (enhanced) |
|
175 enh_str = "enhanced"; |
|
176 else |
|
177 enh_str = ""; |
|
178 endif |
|
179 |
6870
|
180 ## If no 'h' (why not?) then open the terminal as Figure 0. |
|
181 if (isempty (h)) |
|
182 h = 0; |
|
183 endif |
|
184 |
|
185 if (strcmp (term, "x11")) |
7189
|
186 fprintf (plot_stream, "set terminal x11 %s title \"Figure %d\"\n", |
|
187 enh_str, h); |
6870
|
188 elseif (strcmp (term, "aqua")) |
|
189 ## Aqua doesn't understand the 'title' option despite what the |
|
190 ## gnuplot 4.2 documentation says. |
7189
|
191 fprintf (plot_stream, "set terminal aqua %d %s\n", h, enh_str); |
6870
|
192 elseif (strcmp (term, "wxt")) |
7189
|
193 fprintf (plot_stream, "set terminal wxt %s title \"Figure %d\"\n", |
|
194 enh_str, h); |
|
195 |
|
196 elseif (enhanced) |
7211
|
197 fprintf (plot_stream, "set terminal %s %s\n", term, enh_str); |
6870
|
198 endif |
|
199 ## gnuplot will pick up the GNUTERM environment variable itself |
|
200 ## so no need to set the terminal type if not also setting the |
7189
|
201 ## figure title or enhanced mode. |
6870
|
202 |
|
203 endif |
|
204 |
|
205 endfunction |
7189
|
206 |
|
207 function have_enhanced = enhanced_term (term) |
|
208 persistent enhanced_terminals; |
|
209 |
|
210 if (isempty (enhanced_terminals)) |
|
211 ## Don't include pstex, pslatex or epslatex here as the TeX commands |
|
212 ## should not be interpreted in that case. |
|
213 if (compare_versions (__gnuplot_version__ (), "4.0", ">")) |
|
214 enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ... |
11616
|
215 "windows", "wxt", "svg", "postscript", "x11", "pdf"}; |
7189
|
216 else |
|
217 enhanced_terminals = {"x11", "postscript"}; |
|
218 endif |
|
219 endif |
|
220 |
|
221 term = tolower (term); |
|
222 |
|
223 have_enhanced = false; |
|
224 for i = 1 : length (enhanced_terminals) |
|
225 t = enhanced_terminals{i}; |
7191
|
226 if (strncmp (term, t, min (length (term), length (t)))) |
7189
|
227 have_enhanced = true; |
|
228 break; |
|
229 endif |
|
230 endfor |
|
231 endfunction |