GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
__init_gnuplot__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 "build-env.h"
37 #include "builtin-defun-decls.h"
38 #include "defun-dld.h"
39 #include "error.h"
40 #include "file-stat.h"
41 #include "graphics.h"
42 #include "oct-env.h"
43 #include "parse.h"
44 #include "utils.h"
45 #include "variables.h"
46 
47 // PKG_ADD: if (__have_gnuplot__ ()) register_graphics_toolkit ("gnuplot"); endif
48 
49 static bool toolkit_loaded = false;
50 
52 {
53 public:
55  : base_graphics_toolkit ("gnuplot") { }
56 
58 
59  bool is_valid (void) const { return true; }
60 
61  bool initialize (const graphics_object& go)
62  {
63  return go.isa ("figure");
64  }
65 
66  void finalize (const graphics_object& go)
67  {
68  if (go.isa ("figure"))
69  {
70  const figure::properties& props =
71  dynamic_cast<const figure::properties&> (go.get_properties ());
72 
73  send_quit (props.get___plot_stream__ ());
74  }
75  }
76 
77  void update (const graphics_object& go, int id)
78  {
79  if (go.isa ("figure"))
80  {
81  graphics_object obj (go);
82 
83  figure::properties& props =
84  dynamic_cast<figure::properties&> (obj.get_properties ());
85 
86  switch (id)
87  {
89  if (! props.is_visible ())
90  {
91  send_quit (props.get___plot_stream__ ());
92  props.set___plot_stream__ (Matrix ());
93  props.set_graphicssmoothing (false);
94  }
95  break;
96  }
97  }
98  }
99 
100  void redraw_figure (const graphics_object& go) const
101  {
103  args(0) = go.get_handle ().as_octave_value ();
104  feval ("__gnuplot_drawnow__", args);
105  }
106 
107  void print_figure (const graphics_object& go, const std::string& term,
108  const std::string& file,
109  const std::string& debug_file) const
110  {
112  if (! debug_file.empty ())
113  args(3) = debug_file;
114  args(2) = file;
115  args(1) = term;
116  args(0) = go.get_handle ().as_octave_value ();
117  feval ("__gnuplot_drawnow__", args);
118  }
119 
121  {
122  Matrix sz (1, 2, 0.0);
123  return sz;
124  }
125 
126  double get_screen_resolution (void) const
127  { return 72.0; }
128 
129  Matrix get_screen_size (void) const
130  { return Matrix (1, 2, 0.0); }
131 
132  void close (void)
133  {
134  if (toolkit_loaded)
135  {
136  munlock ("__init_gnuplot__");
137 
138  gtk_manager::unload_toolkit ("gnuplot");
139 
140  toolkit_loaded = false;
141  }
142  }
143 
144 private:
145 
146  void send_quit (const octave_value& pstream) const
147  {
148  if (! pstream.is_empty ())
149  {
151  Matrix fids = pstream.matrix_value ();
152 
153  Ffputs (ovl (fids(0), "\nquit;\n"));
154 
155  Ffflush (ovl (fids(0)));
156  Fpclose (ovl (fids(0)));
157 
158  if (fids.numel () > 1)
159  {
160  Fpclose (ovl (fids(1)));
161 
162  if (fids.numel () > 2)
163  Fwaitpid (ovl (fids(2)));
164  }
165  }
166  }
167 };
168 
169 static bool
171 {
172  const std::string exeext = octave::build_env::EXEEXT;
173  const std::string path = octave::sys::env::getenv ("PATH");
174  bool retval = false;
175 
176  try
177  {
178  octave_value_list tmp = feval ("gnuplot_binary", octave_value_list ());
179 
180  if (tmp(0).is_string ())
181  {
182  std::string gnuplot_binary = tmp(0).string_value ();
183 
184  string_vector args (gnuplot_binary);
185  std::string gnuplot_path = search_path_for_file (path, args);
186 
187  octave::sys::file_stat fs (gnuplot_path);
188 
189  if (! fs.exists () && ! exeext.empty ())
190  {
191  args[0] += exeext;
192 
193  gnuplot_path = search_path_for_file (path, args);
194 
195  fs = octave::sys::file_stat (gnuplot_path);
196  }
197 
198  retval = fs.exists ();
199  }
200  }
201  catch (octave::execution_exception&)
202  { }
203 
204  return retval;
205 }
206 
207 // Initialize the gnuplot graphics toolkit.
208 
209 DEFUN_DLD (__init_gnuplot__, , ,
210  doc: /* -*- texinfo -*-
211 @deftypefn {} {} __init_gnuplot__ ()
212 Undocumented internal function.
213 @end deftypefn */)
214 {
216 
217  if (! have_gnuplot_binary ())
218  error ("__init_gnuplot__: the gnuplot program is not available, see 'gnuplot_binary'");
219  else if (! toolkit_loaded)
220  {
221  mlock ();
222 
225 
226  toolkit_loaded = true;
227  }
228 
229  return retval;
230 }
231 
232 DEFUN_DLD (__have_gnuplot__, , ,
233  doc: /* -*- texinfo -*-
234 @deftypefn {} {@var{gnuplot_available} =} __have_gnuplot__ ()
235 Undocumented internal function.
236 @end deftypefn */)
237 {
239 
240  retval = have_gnuplot_binary ();
241 
242  return retval;
243 }
244 
245 /*
246 ## No test needed for internal helper function.
247 %!assert (1)
248 */
OCTINTERP_API octave_value_list Fwaitpid(const octave_value_list &=octave_value_list(), int=0)
static bool have_gnuplot_binary(void)
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
bool is_valid(void) const
bool is_visible(void) const
Definition: graphics.h:2704
Matrix get_screen_size(void) const
bool isa(const std::string &go_name) const
Definition: graphics.h:3286
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
OCTINTERP_API octave_value_list Ffputs(const octave_value_list &=octave_value_list(), int=0)
void print_figure(const graphics_object &go, const std::string &term, const std::string &file, const std::string &debug_file) const
void error(const char *fmt,...)
Definition: error.cc:570
void set_graphicssmoothing(const octave_value &val)
Definition: graphics.h:4265
static void load_toolkit(const graphics_toolkit &tk)
Definition: graphics.h:2266
Matrix get_canvas_size(const graphics_handle &) const
OCTINTERP_API octave_value_list Fpclose(const octave_value_list &=octave_value_list(), int=0)
octave_value get___plot_stream__(void) const
Definition: graphics.h:4139
static std::string getenv(const std::string &name)
Definition: oct-env.cc:235
JNIEnv void * args
Definition: ov-java.cc:67
bool initialize(const graphics_object &go)
static bool toolkit_loaded
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
void redraw_figure(const graphics_object &go) const
static void unload_toolkit(const std::string &name)
Definition: graphics.h:2272
base_properties & get_properties(void)
Definition: graphics.h:3288
OCTINTERP_API void munlock(const std::string &)
bool exists(void) const
Definition: file-stat.h:144
void finalize(const graphics_object &go)
Definition: dMatrix.h:37
sz
Definition: data.cc:5342
double get_screen_resolution(void) const
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
OCTINTERP_API octave_value_list Ffflush(const octave_value_list &=octave_value_list(), int=0)
feval(ar{f}, 1) esult
Definition: oct-parse.cc:8829
std::string search_path_for_file(const std::string &path, const string_vector &names)
Definition: utils.cc:247
const char * EXEEXT
Definition: build-env.cc:116
bool is_empty(void) const
Definition: ov.h:542
void update(const graphics_object &go, int id)
octave::sys::file_stat fs(filename)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Definition: defun-dld.h:45
void set___plot_stream__(const octave_value &val)
Definition: graphics.h:4638
graphics_handle get_handle(void) const
Definition: graphics.h:3274
octave_value as_octave_value(void) const
Definition: oct-handle.h:76
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:854
void send_quit(const octave_value &pstream) const
OCTINTERP_API void mlock(void)