]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/__gnuplot_drawnow__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / __gnuplot_drawnow__.m
1 ## Copyright (C) 2005-2012 John W. Eaton
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} __gnuplot_drawnow__ (@var{h}, @var{term}, @var{file}, @var{mono}, @var{debug_file})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 ## Author: jwe
25
26 function __gnuplot_drawnow__ (h, term, file, mono, debug_file)
27
28   if (nargin < 4)
29     mono = false;
30   endif
31
32   if (nargin >= 3 && nargin <= 5)
33     ## Produce various output formats, or redirect gnuplot stream to a
34     ## debug file.
35     plot_stream = [];
36     fid = [];
37     default_plot_stream = get (h, "__plot_stream__");
38     unwind_protect
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);
44         if (nargin == 5)
45           fid = fopen (debug_file, "wb");
46           enhanced = gnuplot_set_term (fid, true, h, term, file);
47           __go_draw_figure__ (h, fid, enhanced, mono);
48         endif
49       else
50         error ("__gnuplot_drawnow__: the gnuplot terminal, \"%s\", is not available",
51                gnuplot_trim_term (term));
52       endif
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));
59         endif
60         if (numel (plot_stream) > 2)
61           waitpid (plot_stream(3));
62         endif
63       endif
64       if (! isempty (fid))
65         fclose (fid);
66       endif
67     end_unwind_protect
68   elseif (nargin == 1)
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);
73       new_stream = true;
74     else
75       new_stream = false;
76     endif
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, ...
82                                    term, dumb_tmp_file);
83     else
84       enhanced = gnuplot_set_term (plot_stream (1), new_stream, h, term);
85     endif
86     __go_draw_figure__ (h, plot_stream (1), enhanced, mono);
87     fflush (plot_stream (1));
88     if (strcmp (term, "dumb"))
89       fid = -1;
90       while (fid < 0)
91         pause (0.1);
92         fid = fopen (dumb_tmp_file, 'r');
93       endwhile
94       ## reprint the plot on screen
95       [a, count] = fscanf (fid, '%c', Inf);
96       fclose (fid);
97       if (count>0)
98         if (a(1)==12)
99           ## avoid ^L at the beginning
100           a = a(2:end);
101         endif
102         puts (a);
103       endif
104       unlink (dumb_tmp_file);
105     endif
106   else
107     print_usage ();
108   endif
109
110 endfunction
111
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.
115   if (nargin < 4)
116     ## This supports the gnuplot graphics toolkit.
117     term = gnuplot_default_term ();
118     opts_str = "";
119   else
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
122     ## toolkit.
123     [term, opts_str] = gnuplot_trim_term (term);
124     term = lower (term);
125     if (strcmpi (term, "lua"))
126       ## Replace "lau tikz" with
127       term = "tikz";
128       opts_str = strrep (opts_str, "tikz", "");
129     endif
130   endif
131
132   if (strfind (opts_str, "noenhanced"))
133     enhanced = false;
134   else
135     enhanced = gnuplot_is_enhanced_term (term);
136   endif
137
138   ## Set the terminal.
139   if (! isempty (term))
140
141     if (enhanced)
142       enh_str = "enhanced";
143     else
144       enh_str = "";
145     endif
146
147     if (! isempty (h) && isfigure (h))
148
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);
155         else
156           title_str = "";
157         endif
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;
162         endif
163         if (! isempty (title_str))
164           title_str = sprintf ("title \"%s\"", title_str);
165         endif
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);
171         endif
172       else
173         title_str = "";
174       endif
175
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");
180         unwind_protect
181           set (h, "units", "pixels");
182           position_in_pixels = get (h, "position");
183         unwind_protect_cleanup
184           set (h, "units", units);
185         end_unwind_protect
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"}))))
191           ## Convert to inches
192           gnuplot_pos = gnuplot_pos / 72;
193           gnuplot_size = gnuplot_size / 72;
194         endif
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";
202           endif
203           if (__gnuplot_has_feature__ ("wxt_figure_size"))
204             terminals_with_size{end+1} = "wxt";
205           endif
206           switch (term)
207           case terminals_with_size
208             size_str = sprintf ("size %g,%g", gnuplot_size);
209           case "tikz"
210             size_str = sprintf ("size %gin,%gin", gnuplot_size);
211           case "dumb"
212             new_stream = 1;
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];
218             else
219               ## Use the gnuplot default.
220               size_str = "";
221             endif
222           case {"aqua", "fig", "corel"}
223             size_str = sprintf ("size %g %g", gnuplot_size);
224           case "dxf"
225             size_str = "";
226           otherwise
227             size_str = "";
228           endswitch
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");
233             unwind_protect
234               set (0, "units", "pixels");
235               screen_size = get (0, "screensize")(3:4);
236             unwind_protect_cleanup
237               set (0, "units", units);
238             end_unwind_protect
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));
246             endif
247           endif
248         else
249           size_str = "";
250           warning ("gnuplot_set_term: size is zero");
251         endif
252       else
253         ## A specified size take priority over the figure properies.
254         size_str = "";
255       endif
256     else
257       if isempty (h)
258         disp ("gnuplot_set_term: figure handle is empty");
259       elseif !isfigure(h)
260         disp ("gnuplot_set_term: not a figure handle");
261       endif
262       title_str = "";
263       size_str = "";
264     endif
265
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);
270     endif
271     if (! isempty (title_str))
272       term_str = sprintf ("%s %s", term_str, title_str);
273     endif
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);
279       endif
280       if (nargin > 3 && ischar (opts_str))
281         ## Options must go last.
282         term_str = sprintf ("%s %s", term_str, opts_str);
283       endif
284     else
285       if (nargin > 3 && ischar (opts_str))
286         ## Options must go last.
287         term_str = sprintf ("%s %s", term_str, opts_str);
288       endif
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);
293       endif
294     endif
295
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);
305       if (nargin == 5)
306         if (! isempty (file))
307           fprintf (plot_stream, "set output '%s';\n", file);
308         endif
309       endif
310       fputs (plot_stream, "set multiplot;\n");
311     elseif (any (strcmp (term, flickering_terms)))
312       fprintf (plot_stream, "%s\n", term_str);
313       if (nargin == 5)
314         if (! isempty (file))
315           fprintf (plot_stream, "set output '%s';\n", file);
316         endif
317       endif
318     endif
319   else
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.
323   endif
324
325 endfunction
326
327 function term = gnuplot_default_term ()
328   term = getenv ("GNUTERM");
329   ## If not specified, guess the terminal type.
330   if (isempty (term))
331     if (ismac ())
332       term = "aqua";
333     elseif (! isunix ())
334       term = "windows";
335     elseif (! isempty (getenv ("DISPLAY")))
336       term = "x11";
337     else
338       term = "dumb";
339     endif
340   endif
341 endfunction
342
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, ' ');
347   if (isempty (n))
348     term = string;
349     opts = "";
350   else
351     term = string(1:(n-1));
352     opts = string((n+1):end);
353   endif
354 endfunction
355
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"};
364   endif
365   if (nargin < 1)
366     ## Determine the default gnuplot terminal.
367     term = gnuplot_default_term ();
368   endif
369   have_enhanced = any (strncmp (enhanced_terminals, term, min (numel (term), 3)));
370 endfunction
371
372 function ret = output_to_screen (term)
373   ret = any (strcmpi ({"aqua", "dumb", "wxt", "x11", "windows", "pm"}, term));
374 endfunction
375
376 function retval = have_non_legend_axes (h)
377   retval = false;
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;
386     endif
387   endif
388 endfunction
389
390
391 ## No test needed for internal helper function.
392 %!assert (1)