GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__init_gnuplot__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 /*
24 
25 To initialize:
26 
27  graphics_toolkit ("gnuplot");
28  plot (randn (1e3, 1));
29 
30 */
31 
32 #if defined (HAVE_CONFIG_H)
33 # include "config.h"
34 #endif
35 
36 #include <string>
37 
38 #include "dMatrix.h"
39 #include "file-stat.h"
40 #include "oct-env.h"
41 
42 #include "build-env.h"
43 #include "builtin-defun-decls.h"
44 #include "defun-dld.h"
45 #include "error.h"
46 #include "graphics.h"
47 #include "ov.h"
48 #include "ovl.h"
49 #include "parse.h"
50 #include "utils.h"
51 #include "variables.h"
52 
53 // PKG_ADD: if (__have_gnuplot__ ()) register_graphics_toolkit ("gnuplot"); endif
54 
55 static bool toolkit_loaded = false;
56 
58 {
59 public:
61  : base_graphics_toolkit ("gnuplot"), m_interpreter (interp) { }
62 
63  ~gnuplot_graphics_toolkit (void) = default;
64 
65  bool is_valid (void) const { return true; }
66 
67  bool initialize (const graphics_object& go)
68  {
69  return go.isa ("figure");
70  }
71 
72  void finalize (const graphics_object& go)
73  {
74  if (go.isa ("figure"))
75  {
76  const figure::properties& props =
77  dynamic_cast<const figure::properties&> (go.get_properties ());
78 
79  send_quit (props.get___plot_stream__ ());
80  }
81  }
82 
83  void update (const graphics_object& go, int id)
84  {
85  if (go.isa ("figure"))
86  {
87  graphics_object obj (go);
88 
89  figure::properties& props =
90  dynamic_cast<figure::properties&> (obj.get_properties ());
91 
92  switch (id)
93  {
94  case base_properties::ID_VISIBLE:
95  if (! props.is_visible ())
96  {
97  send_quit (props.get___plot_stream__ ());
98  props.set___plot_stream__ (Matrix ());
99  props.set_graphicssmoothing (false);
100  }
101  break;
102  }
103  }
104  }
105 
106  void redraw_figure (const graphics_object& go) const
107  {
108  octave_value_list args;
109  args(0) = go.get_handle ().as_octave_value ();
110  octave::feval ("__gnuplot_drawnow__", args);
111  }
112 
113  void print_figure (const graphics_object& go, const std::string& term,
114  const std::string& file,
115  const std::string& debug_file) const
116  {
117  octave_value_list args;
118  if (! debug_file.empty ())
119  args(3) = debug_file;
120  args(2) = file;
121  args(1) = term;
122  args(0) = go.get_handle ().as_octave_value ();
123  octave::feval ("__gnuplot_drawnow__", args);
124  }
125 
127  {
128  Matrix sz (1, 2, 0.0);
129  return sz;
130  }
131 
132  double get_screen_resolution (void) const
133  { return 72.0; }
134 
135  Matrix get_screen_size (void) const
136  { return Matrix (1, 2, 0.0); }
137 
138  void close (void)
139  {
140  if (toolkit_loaded)
141  {
142  m_interpreter.munlock ("__init_gnuplot__");
143 
144  toolkit_loaded = false;
145  }
146  }
147 
148 private:
149 
150  void send_quit (const octave_value& pstream) const
151  {
152  if (! pstream.isempty ())
153  {
154  octave_value_list args;
155  Matrix fids = pstream.matrix_value ();
156 
157  Ffputs (m_interpreter, ovl (fids(0), "\nquit;\n"));
158 
159  Ffflush (m_interpreter, ovl (fids(0)));
160  Fpclose (m_interpreter, ovl (fids(0)));
161 
162  if (fids.numel () > 1)
163  {
164  Fpclose (m_interpreter, ovl (fids(1)));
165 
166  if (fids.numel () > 2)
167  Fwaitpid (ovl (fids(2)));
168  }
169  }
170  }
171 
173 };
174 
175 static bool
177 {
178  const std::string exeext = octave::build_env::EXEEXT;
179  const std::string path = octave::sys::env::getenv ("PATH");
180  bool retval = false;
181 
182  try
183  {
185  = octave::feval ("gnuplot_binary", octave_value_list ());
186 
187  if (tmp(0).is_string ())
188  {
189  std::string gnuplot_binary = tmp(0).string_value ();
190 
191  string_vector args (gnuplot_binary);
192  std::string gnuplot_path = search_path_for_file (path, args);
193 
194  octave::sys::file_stat fs (gnuplot_path);
195 
196  if (! fs.exists () && ! exeext.empty ())
197  {
198  args[0] += exeext;
199 
200  gnuplot_path = search_path_for_file (path, args);
201 
202  fs = octave::sys::file_stat (gnuplot_path);
203  }
204 
205  retval = fs.exists ();
206  }
207  }
208  catch (octave::execution_exception&)
209  { }
210 
211  return retval;
212 }
213 
214 // Initialize the gnuplot graphics toolkit.
215 
216 DEFMETHOD_DLD (__init_gnuplot__, interp, , ,
217  doc: /* -*- texinfo -*-
218 @deftypefn {} {} __init_gnuplot__ ()
219 Undocumented internal function.
220 @end deftypefn */)
221 {
222  if (! have_gnuplot_binary ())
223  error ("__init_gnuplot__: the gnuplot program is not available, see 'gnuplot_binary'");
224  else if (! toolkit_loaded)
225  {
226  interp.mlock ();
227 
228  octave::gtk_manager& gtk_mgr = interp.get_gtk_manager ();
229 
230  graphics_toolkit tk (new gnuplot_graphics_toolkit (interp));
231  gtk_mgr.load_toolkit (tk);
232 
233  toolkit_loaded = true;
234  }
235 
236  return octave_value_list ();
237 }
238 
239 DEFUN_DLD (__have_gnuplot__, , ,
240  doc: /* -*- texinfo -*-
241 @deftypefn {} {@var{gnuplot_available} =} __have_gnuplot__ ()
242 Undocumented internal function.
243 @end deftypefn */)
244 {
245  return ovl (have_gnuplot_binary ());
246 }
247 
248 /*
249 ## No test needed for internal helper function.
250 %!assert (1)
251 */
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
static bool have_gnuplot_binary(void)
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
void load_toolkit(const graphics_toolkit &tk)
Definition: gtk-manager.h:54
~gnuplot_graphics_toolkit(void)=default
bool isempty(void) const
Definition: ov.h:529
graphics_handle get_handle(void) const
Definition: graphics.in.h:2774
Matrix get_screen_size(void) const
void error(const char *fmt,...)
Definition: error.cc:578
octave::interpreter & m_interpreter
Matrix get_canvas_size(const graphics_handle &) const
static std::string getenv(const std::string &name)
Definition: oct-env.cc:235
bool isa(const std::string &go_name) const
Definition: graphics.in.h:2786
bool initialize(const graphics_object &go)
#define DEFMETHOD_DLD(name, interp_name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin method.
Definition: defun-dld.h:96
void print_figure(const graphics_object &go, const std::string &term, const std::string &file, const std::string &debug_file) const
static bool toolkit_loaded
octave_value as_octave_value(void) const
Definition: oct-handle.h:76
void munlock(const std::string &nm)
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
double get_screen_resolution(void) const
base_properties & get_properties(void)
Definition: graphics.in.h:2788
void finalize(const graphics_object &go)
Definition: dMatrix.h:36
sz
Definition: data.cc:5264
std::string search_path_for_file(const std::string &path, const string_vector &names)
Definition: utils.cc:247
const char * EXEEXT
void redraw_figure(const graphics_object &go) const
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
void send_quit(const octave_value &pstream) const
gnuplot_graphics_toolkit(octave::interpreter &interp)
void update(const graphics_object &go, int id)
bool exists(void) const
Definition: file-stat.h:144
octave::sys::file_stat fs(filename)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Macro to define an at run time dynamically loadable builtin function.
Definition: defun-dld.h:58
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:834