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