
## -*- texinfo -*-
## @deftypefn  {Function File} {} test_printing ()
## @deftypefnx {Function File} {} test_printing (@var{backend}, @var{print_opt})
## @deftypefnx {Function File} {} test_printing (@var{backend}, @var{print_opt1}, @var{print_opt2}, @dots{})
## Function to test the @code{print} command for the FLTK and Gnuplot backends.
##
## If a backend is not specified both the FLTK and Gnuplot backends will be tested.
##
## If the output device is not specified a default list is used.
## @end deftypefn

function test_printing (varargin)

  print_opts = varargin(strncmp(varargin,"-",1));
  backends = varargin(!strncmp(varargin,"-",1));
  devices0 = print_opts(strncmp(print_opts,"-d",2) & !strcmp(print_opts,"-debug"));
  devices0 = strrep (devices0, "-d", "");
  print_opts(strncmp(print_opts,"-d",2) & ! strcmp (print_opts, "-debug")) = [];

  if (isempty (backends))
    backends = {"fltk", "gnuplot"};
  end

  if (isempty (devices0))
    devices0 = {"epsc"; "psc"; "pdf"; "png"; "jpg"; "svg"; "dxf"; "emf";
               "hpgl"; "pstex"; "epslatex"; "tikz"};
    fltk_devices = {"aifm", "pdflatex", "pslatex"};
    gnuplot_devices = {"gif"; "corel"; "eepic"; "pdfcairo";
                       "pngcairo"; "pstricks"; "texdraw"};
  else
    fltk_devices = {};
    gnuplot_devices = {};
  endif

  x = 0:0.1:10;
  more off
  for n = 1:numel(backends)
    close all
    backend (backends{n})
    figure (1)
    drawnow ();
    fprintf ("backend is %s\n", backends{n})
    if (strcmp (backends{n}, "fltk"))
      devices = union (devices0, fltk_devices);
    else
      devices = union (devices0, gnuplot_devices);
    endif
    plot (x, sin(x), x, cos(x))
    xlabel ("Angle, radians")
    ylabel ("Amplitude")
    title ("Triginometric Functions")
    legend ({"sin(x)", "cos(x)"}, "location", "northwest")
    for m = 1:numel(devices)
      if (strcmp (backends{n}, "fltk")
          || __gnuplot_has_terminal__ (devices{m})
          || any (strcmp ({"eps", "eps2", "epsc", "epsc2", ...
                           "ps", "ps2", "psc", "psc2", "jpg"},
                          devices{m})))
        filename = sprintf ("%s-%s", backends{n}, devices{m});
        dev_opt = sprintf (" -d%s", devices{m});
        fprintf ("print %s", filename);
        fprintf (" %s", dev_opt, print_opts{:});
        fprintf ("\n");
        print (filename, dev_opt, print_opts{:});
        drawnow ("expose")
        if (any (strcmpi (devices{m}, {"psc", "pdf"})))
          fprintf ("print %s", filename);
          fprintf (" %s", dev_opt, print_opts{:});
          fprintf (" -append\n");
          print (filename, dev_opt, print_opts{:}, "-append");
          drawnow ("expose")
        elseif (strncmpi (devices{m}, "eps", 3))
          print (strcat (filename, "-tight"), dev_opt, print_opts{:}, "-tight");
          print (strcat (filename, "-tiff"), dev_opt, print_opts{:}, "-tiff");
        endif
      else
        fprintf ("Gnuplot does not have support for '%s' ... skipping.\n",
                 devices{m})
      endif
    endfor
  endfor

endfunction

function gnuplot_supports_term = __gnuplot_has_terminal__ (term, plot_stream)
  term = deblank (term);
  n = find (term == " ", 1);
  if (! isempty (n))
    term = term(1:n-1);
  endif
  if (__gnuplot_has_feature__ ("variable_GPVAL_TERMINALS"))
    if (nargin < 2)
      plot_stream = __gnuplot_open_stream__ (2);
    endif
    available_terminals = __gnuplot_get_var__ (plot_stream, "GPVAL_TERMINALS");
    available_terminals = regexp (available_terminals, "\\b\\w+\\b", "match");
    if (nargin < 2 && ! isempty (plot_stream))
      pclose (plot_stream(1));
      if (numel (plot_stream) > 1)
        pclose (plot_stream(2));
      endif
      if (numel (plot_stream) > 2)
        waitpid (plot_stream(3));
      endif
    endif
  else
    ## Gnuplot 4.0 terminals. No new terminals were added until 4.4 which
    ## allows the list of terminals to be obtained from GPVAL_TERMINALS.
    available_terminals = {"aifm", "aqua", "canvas", "cgm", "corel", ...
                           "dumb", "dxf", "eepic", "emf", "epslatex", ...
                           "epson_180dpi", "fig", "gif", "gnugraph", ...
                           "gpic", "hp2623A", "hp2648", "hp500c", ...
                           "hpgl", "hpljii", "hppj", "imagen", "jpeg", ...
                           "latex", "mf", "mif", "mp", "pbm", "pdf", ...
                           "pm", "png", "postscript", "pslatex", ...
                           "pstex", "pstricks", "qms", "regis", "rgip", ...
                           "svg", "texdraw", "tgif", "tkcanvas", ...
                           "tpic", "windows", "x11", "xlib", "xterm"};
  endif
  gnuplot_supports_term = any (strcmpi (available_terminals, term));
endfunction

