1 ## Copyright (C) 2005-2012 John W. Eaton
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {} __gnuplot_drawnow__ (@var{h}, @var{term}, @var{file}, @var{mono}, @var{debug_file})
21 ## Undocumented internal function.
26 function __gnuplot_drawnow__ (h, term, file, mono, debug_file)
32 if (nargin >= 3 && nargin <= 5)
33 ## Produce various output formats, or redirect gnuplot stream to a
37 default_plot_stream = get (h, "__plot_stream__");
39 plot_stream = __gnuplot_open_stream__ (2, h);
40 gnuplot_supports_term = __gnuplot_has_terminal__ (term, plot_stream);
41 if (gnuplot_supports_term)
42 enhanced = gnuplot_set_term (plot_stream (1), true, h, term, file);
43 __go_draw_figure__ (h, plot_stream(1), enhanced, mono);
45 fid = fopen (debug_file, "wb");
46 enhanced = gnuplot_set_term (fid, true, h, term, file);
47 __go_draw_figure__ (h, fid, enhanced, mono);
50 error ("__gnuplot_drawnow__: the gnuplot terminal, \"%s\", is not available",
51 gnuplot_trim_term (term));
53 unwind_protect_cleanup
54 set (h, "__plot_stream__", default_plot_stream);
55 if (! isempty (plot_stream))
56 pclose (plot_stream(1));
57 if (numel (plot_stream) > 1)
58 pclose (plot_stream(2));
60 if (numel (plot_stream) > 2)
61 waitpid (plot_stream(3));
69 ## Graphics terminal for display.
70 plot_stream = get (h, "__plot_stream__");
71 if (isempty (plot_stream))
72 plot_stream = __gnuplot_open_stream__ (2, h);
77 term = gnuplot_default_term ();
78 if (strcmp (term, "dumb"))
79 ## popen2 eats stdout of gnuplot, use temporary file instead
80 dumb_tmp_file = tmpnam ();
81 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, ...
84 enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, term);
86 __go_draw_figure__ (h, plot_stream (1), enhanced, mono);
87 fflush (plot_stream (1));
88 if (strcmp (term, "dumb"))
92 fid = fopen (dumb_tmp_file, 'r');
94 ## reprint the plot on screen
95 [a, count] = fscanf (fid, '%c', Inf);
99 ## avoid ^L at the beginning
104 unlink (dumb_tmp_file);
112 function enhanced = gnuplot_set_term (plot_stream, new_stream, h, term, file)
113 ## Generate the gnuplot "set terminal <term> ..." command.
114 ## When "term" originates from print.m, it may include other options.
116 ## This supports the gnuplot graphics toolkit.
117 term = gnuplot_default_term ();
120 ## Get the one word terminal id and save the remaining as options to
121 ## be passed on to gnuplot. The terminal may respect the graphics
123 [term, opts_str] = gnuplot_trim_term (term);
125 if (strcmpi (term, "lua"))
126 ## Replace "lau tikz" with
128 opts_str = strrep (opts_str, "tikz", "");
132 if (strfind (opts_str, "noenhanced"))
135 enhanced = gnuplot_is_enhanced_term (term);
139 if (! isempty (term))
142 enh_str = "enhanced";
147 if (! isempty (h) && isfigure (h))
149 ## Generate gnuplot title string for plot windows.
150 if (output_to_screen (term) && ~strcmp (term, "dumb"))
151 fig.numbertitle = get (h, "numbertitle");
152 fig.name = strrep (get (h, "name"), "\"", "\\\"");
153 if (strcmpi (get (h, "numbertitle"), "on"))
154 title_str = sprintf ("Figure %d", h);
158 if (! isempty (fig.name) && ! isempty (title_str))
159 title_str = sprintf ("%s: %s", title_str, fig.name);
160 elseif (! isempty (fig.name) && isempty (title_str))
161 title_str = fig.name;
163 if (! isempty (title_str))
164 title_str = sprintf ("title \"%s\"", title_str);
166 if (strcmp (term, "aqua"))
167 ## Adjust axes-label and tick-label spacing.
168 opts_str = sprintf ("%s font \"%s,%d\"", opts_str,
169 get (0, "defaultaxesfontname"),
170 get (0, "defaultaxesfontsize") / 1.5);
176 if (! (any (strfind (opts_str, " size ") > 0)
177 || any (strfind (opts_str, "size ") == 1)))
178 ## Get figure size in pixels. Rely on listener to handle coversion.
179 units = get (h, "units");
181 set (h, "units", "pixels");
182 position_in_pixels = get (h, "position");
183 unwind_protect_cleanup
184 set (h, "units", units);
186 gnuplot_pos = position_in_pixels(1:2);
187 gnuplot_size = position_in_pixels(3:4);
188 if (! (output_to_screen (term)
189 || any (strcmp (term, {"emf", "gif", "jpeg", "pbm", "png", ...
190 "pngcairo", "svg"}))))
192 gnuplot_pos = gnuplot_pos / 72;
193 gnuplot_size = gnuplot_size / 72;
195 if (all (gnuplot_size > 0))
196 terminals_with_size = {"canvas", "emf", "epslatex", "fig", ...
197 "gif", "jpeg", "latex", "pbm", "pdf", ...
198 "pdfcairo", "postscript", "png", "pngcairo", ...
199 "pstex", "pslatex", "svg", "tikz"};
200 if (__gnuplot_has_feature__ ("x11_figure_position"))
201 terminals_with_size{end+1} = "x11";
203 if (__gnuplot_has_feature__ ("wxt_figure_size"))
204 terminals_with_size{end+1} = "wxt";
207 case terminals_with_size
208 size_str = sprintf ("size %g,%g", gnuplot_size);
210 size_str = sprintf ("size %gin,%gin", gnuplot_size);
213 if (~isempty (getenv ("COLUMNS")) && ~isempty (getenv ("LINES")))
214 ## Let dumb use full text screen size (minus prompt lines).
215 n = sprintf ("%i", -2 - length (find (sprintf ("%s", PS1) == "\n")));
216 ## n = the number of times \n appears in PS1
217 size_str = ["size ", getenv("COLUMNS"), ",", getenv("LINES"), n];
219 ## Use the gnuplot default.
222 case {"aqua", "fig", "corel"}
223 size_str = sprintf ("size %g %g", gnuplot_size);
229 if (strncmpi (term, "x11", 3)
230 && __gnuplot_has_feature__ ("x11_figure_position"))
231 ## X11 allows the window to be positioned as well.
232 units = get (0, "units");
234 set (0, "units", "pixels");
235 screen_size = get (0, "screensize")(3:4);
236 unwind_protect_cleanup
237 set (0, "units", units);
239 if (all (screen_size > 0))
240 ## For X11, set the figure positon as well as the size
241 ## gnuplot position is UL, Octave's is LL (same for screen/window)
242 gnuplot_pos(2) = screen_size(2) - gnuplot_pos(2) - gnuplot_size(2);
243 gnuplot_pos = max (gnuplot_pos, 1);
244 size_str = sprintf ("%s position %d,%d", size_str,
245 gnuplot_pos(1), gnuplot_pos(2));
250 warning ("gnuplot_set_term: size is zero");
253 ## A specified size take priority over the figure properies.
258 disp ("gnuplot_set_term: figure handle is empty");
260 disp ("gnuplot_set_term: not a figure handle");
266 ## Set the gnuplot terminal (type, enhanced, title, options & size).
267 term_str = sprintf ("set terminal %s", term);
268 if (! isempty (enh_str))
269 term_str = sprintf ("%s %s", term_str, enh_str);
271 if (! isempty (title_str))
272 term_str = sprintf ("%s %s", term_str, title_str);
274 if (isempty (strfind (term, "corel")))
275 if (! isempty (size_str) && new_stream)
276 ## size_str comes after other options to permit specification of
277 ## the canvas size for terminals cdr/corel.
278 term_str = sprintf ("%s %s", term_str, size_str);
280 if (nargin > 3 && ischar (opts_str))
281 ## Options must go last.
282 term_str = sprintf ("%s %s", term_str, opts_str);
285 if (nargin > 3 && ischar (opts_str))
286 ## Options must go last.
287 term_str = sprintf ("%s %s", term_str, opts_str);
289 if (! isempty (size_str) && new_stream)
290 ## size_str comes after other options to permit specification of
291 ## the canvas size for terminals cdr/corel.
292 term_str = sprintf ("%s %s", term_str, size_str);
296 ## Work around the gnuplot feature of growing the x11 window and
297 ## flickering window (x11, windows, & wxt) when the mouse and
298 ## multiplot are set in gnuplot.
299 fputs (plot_stream, "unset multiplot;\n");
300 flickering_terms = {"x11", "windows", "wxt", "dumb"};
301 if (! any (strcmp (term, flickering_terms))
302 || have_non_legend_axes (h)
303 || numel (findall (h, "type", "image")) > 0)
304 fprintf (plot_stream, "%s\n", term_str);
306 if (! isempty (file))
307 fprintf (plot_stream, "set output '%s';\n", file);
310 fputs (plot_stream, "set multiplot;\n");
311 elseif (any (strcmp (term, flickering_terms)))
312 fprintf (plot_stream, "%s\n", term_str);
314 if (! isempty (file))
315 fprintf (plot_stream, "set output '%s';\n", file);
320 ## gnuplot will pick up the GNUTERM environment variable itself
321 ## so no need to set the terminal type if not also setting the
322 ## figure title, enhanced mode, or position.
327 function term = gnuplot_default_term ()
328 term = getenv ("GNUTERM");
329 ## If not specified, guess the terminal type.
335 elseif (! isempty (getenv ("DISPLAY")))
343 function [term, opts] = gnuplot_trim_term (string)
344 ## Extract the terminal type and terminal options (from print.m)
345 string = deblank (string);
346 n = strfind (string, ' ');
351 term = string(1:(n-1));
352 opts = string((n+1):end);
356 function have_enhanced = gnuplot_is_enhanced_term (term)
357 persistent enhanced_terminals;
358 if (isempty (enhanced_terminals))
359 ## Don't include pstex, pslatex or epslatex here as the TeX commands
360 ## should not be interpreted in that case.
361 enhanced_terminals = {"aqua", "canvas", "dumb", "emf", "gif", "jpeg", ...
362 "pdf", "pdfcairo", "pm", "png", "pngcairo", ...
363 "postscript", "svg", "windows", "wxt", "x11"};
366 ## Determine the default gnuplot terminal.
367 term = gnuplot_default_term ();
369 have_enhanced = any (strncmp (enhanced_terminals, term, min (numel (term), 3)));
372 function ret = output_to_screen (term)
373 ret = any (strcmpi ({"aqua", "dumb", "wxt", "x11", "windows", "pm"}, term));
376 function retval = have_non_legend_axes (h)
378 all_axes = findall (h, "type", "axes");
379 if (! isempty (all_axes))
380 n_all_axes = numel (all_axes);
381 all_axes_tags = get (all_axes, "tag");
382 legend_axes = strcmp (all_axes_tags, "legend");
383 if (! isempty (legend_axes))
384 n_legend_axes = sum (legend_axes);
385 retval = (n_all_axes - n_legend_axes) > 1;
391 ## No test needed for internal helper function.