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 |
|
48 plot_stream = open_gnuplot_stream ([], term, file); |
|
49 __go_draw_figure__ (f, plot_stream); |
6870
|
50 if (nargin == 3) |
|
51 fid = fopen (debug_file, "wb"); |
|
52 init_plot_stream (fid, [], term, file); |
|
53 __go_draw_figure__ (f, fid); |
|
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)) |
|
75 plot_stream = open_gnuplot_stream (h); |
|
76 endif |
|
77 __go_draw_figure__ (f, plot_stream); |
|
78 elseif (! isempty (plot_stream)) |
|
79 pclose (plot_stream); |
|
80 set (h, "__plot_stream__", []); |
|
81 endif |
|
82 set (h, "__modified__", false); |
|
83 endif |
|
84 endif |
|
85 endfor |
|
86 else |
|
87 print_usage (); |
|
88 endif |
6425
|
89 |
6454
|
90 unwind_protect_cleanup |
|
91 |
|
92 drawnow_executing--; |
|
93 __request_drawnow__ (false); |
|
94 |
|
95 end_unwind_protect |
6425
|
96 |
|
97 endfunction |
|
98 |
6870
|
99 function plot_stream = open_gnuplot_stream (h, varargin) |
6425
|
100 |
6418
|
101 ## If drawnow is cleared, it is possible to register __go_close_all__ |
|
102 ## more than once, but that is not fatal. |
|
103 persistent __go_close_all_registered__; |
6298
|
104 |
6425
|
105 cmd = gnuplot_binary (); |
6257
|
106 |
6425
|
107 plot_stream = popen (cmd, "w"); |
|
108 |
|
109 if (plot_stream < 0) |
|
110 error ("drawnow: failed to open connection to gnuplot"); |
|
111 else |
|
112 |
|
113 if (! isempty (h)) |
|
114 set (h, "__plot_stream__", plot_stream); |
6257
|
115 endif |
|
116 |
6870
|
117 init_plot_stream (plot_stream, h, varargin{:}) |
6257
|
118 |
6425
|
119 if (isempty (__go_close_all_registered__)) |
|
120 atexit ("__go_close_all__"); |
|
121 __go_close_all_registered__ = true; |
6257
|
122 endif |
|
123 |
|
124 endif |
|
125 |
|
126 endfunction |
6870
|
127 |
|
128 function init_plot_stream (plot_stream, h, term, file) |
|
129 |
|
130 if (nargin == 4) |
|
131 if (! isempty (term)) |
|
132 fprintf (plot_stream, "set terminal %s;\n", term); |
|
133 endif |
|
134 if (! isempty (file)) |
|
135 fprintf (plot_stream, "set output \"%s\";\n", file); |
|
136 endif |
|
137 else |
|
138 |
|
139 ## Guess the terminal type. |
|
140 term = getenv ("GNUTERM"); |
|
141 if (isempty (term)) |
|
142 if (! isempty (getenv ("DISPLAY"))) |
|
143 term = "x11"; |
|
144 elseif (! isunix ()) |
|
145 term = "windows"; |
|
146 else |
|
147 ## This should really be checking for os x before setting |
|
148 ## the terminal type to aqua, but nobody will notice because |
|
149 ## every other unix will be using x11 and windows will be |
|
150 ## using windows. Those diehards still running octave from |
|
151 ## a linux console know how to set the GNUTERM variable. |
|
152 term = "aqua"; |
|
153 endif |
|
154 endif |
|
155 |
|
156 ## If no 'h' (why not?) then open the terminal as Figure 0. |
|
157 if (isempty (h)) |
|
158 h = 0; |
|
159 endif |
|
160 |
|
161 if (strcmp (term, "x11")) |
|
162 fprintf (plot_stream, "set terminal x11 title \"Figure %d\"\n", h); |
|
163 elseif (strcmp (term, "aqua")) |
|
164 ## Aqua doesn't understand the 'title' option despite what the |
|
165 ## gnuplot 4.2 documentation says. |
|
166 fprintf (plot_stream, "set terminal aqua %d\n", h); |
|
167 elseif (strcmp (term, "wxt")) |
|
168 fprintf (plot_stream, "set terminal wxt title \"Figure %d\"\n", h); |
|
169 endif |
|
170 ## gnuplot will pick up the GNUTERM environment variable itself |
|
171 ## so no need to set the terminal type if not also setting the |
|
172 ## figure title. |
|
173 |
|
174 endif |
|
175 |
|
176 endfunction |