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