GNU Octave  4.0.0
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-2015 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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include "builtins.h"
37 #include "defun-dld.h"
38 #include "error.h"
39 #include "file-stat.h"
40 #include "graphics.h"
41 #include "oct-conf.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___enhanced__ (false);
94  }
95  break;
96  }
97  }
98  }
99 
100  void redraw_figure (const graphics_object& go) const
101  {
102  octave_value_list args;
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, bool mono,
109  const std::string& debug_file) const
110  {
111  octave_value_list args;
112  if (! debug_file.empty ())
113  args(4) = debug_file;
114  args(3) = mono;
115  args(2) = file;
116  args(1) = term;
117  args(0) = go.get_handle ().as_octave_value ();
118  feval ("__gnuplot_drawnow__", args);
119  }
120 
122  {
123  Matrix sz (1, 2, 0.0);
124  return sz;
125  }
126 
127  double get_screen_resolution (void) const
128  { return 72.0; }
129 
130  Matrix get_screen_size (void) const
131  { return Matrix (1, 2, 0.0); }
132 
133  void close (void)
134  {
135  if (toolkit_loaded)
136  {
137  munlock ("__init_gnuplot__");
138 
139  gtk_manager::unload_toolkit ("gnuplot");
140 
141  toolkit_loaded = false;
142  }
143  }
144 
145 private:
146 
147  void send_quit (const octave_value& pstream) const
148  {
149  if (! pstream.is_empty ())
150  {
151  octave_value_list args;
152  Matrix fids = pstream.matrix_value ();
153 
154  if (! error_state)
155  {
156  Ffputs (ovl (fids(0), "\nquit;\n"));
157 
158  Ffflush (ovl (fids(0)));
159  Fpclose (ovl (fids(0)));
160 
161  if (fids.numel () > 1)
162  {
163  Fpclose (ovl (fids(1)));
164 
165  if (fids.numel () > 2)
166  Fwaitpid (ovl (fids(2)));
167  }
168  }
169  }
170  }
171 };
172 
173 static bool
175 {
176  const std::string exeext = std::string (OCTAVE_CONF_EXEEXT);
177  const std::string path = octave_env::getenv ("PATH");
178 
179  octave_value_list tmp = feval ("gnuplot_binary", octave_value_list ());
180  std::string gnuplot_binary = tmp(0).string_value ();
181 
182  string_vector args (gnuplot_binary);
183  std::string gnuplot_path = search_path_for_file (path, args);
184 
185  file_stat fs (gnuplot_path);
186 
187  if (! fs.exists () && ! exeext.empty ())
188  {
189  args[0] += exeext;
190 
191  gnuplot_path = search_path_for_file (path, args);
192 
193  fs = file_stat (gnuplot_path);
194  }
195 
196  return fs.exists ();
197 }
198 
199 // Initialize the gnuplot graphics toolkit.
200 
201 DEFUN_DLD (__init_gnuplot__, , , "")
202 {
203  octave_value retval;
204 
205  if (! have_gnuplot_binary ())
206  error ("__init_gnuplot__: the gnuplot program is not available, see 'gnuplot_binary'");
207  else if (! toolkit_loaded)
208  {
209  mlock ();
210 
213 
214  toolkit_loaded = true;
215  }
216 
217  return retval;
218 }
219 
220 DEFUN_DLD (__have_gnuplot__, , ,
221  "-*- texinfo -*-\n\
222 @deftypefn {Loadable Function} {@var{gnuplot_available} =} __have_gnuplot__ ()\n\
223 Undocumented internal function.\n\
224 @end deftypefn")
225 {
226  octave_value retval;
227 
228  retval = have_gnuplot_binary ();
229 
230  return retval;
231 }
232 
void set___enhanced__(const octave_value &val)
Definition: graphics.h:4851
OCTINTERP_API octave_value_list Fwaitpid(const octave_value_list &=octave_value_list(), int=0)
Definition: syscalls.cc:1397
static bool have_gnuplot_binary(void)
bool is_valid(void) const
bool is_visible(void) const
Definition: graphics.h:2758
Matrix get_screen_size(void) const
bool isa(const std::string &go_name) const
Definition: graphics.h:3375
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
OCTINTERP_API octave_value_list Ffputs(const octave_value_list &=octave_value_list(), int=0)
Definition: file-io.cc:1027
void error(const char *fmt,...)
Definition: error.cc:476
octave_value_list feval(const std::string &name, const octave_value_list &args, int nargout)
Definition: oct-parse.cc:8625
static void load_toolkit(const graphics_toolkit &tk)
Definition: graphics.h:2313
Matrix get_canvas_size(const graphics_handle &) const
OCTINTERP_API octave_value_list Fpclose(const octave_value_list &=octave_value_list(), int=0)
Definition: file-io.cc:1953
octave_value get___plot_stream__(void) const
Definition: graphics.h:4306
bool initialize(const graphics_object &go)
bool exists(void) const
Definition: file-stat.h:134
static bool toolkit_loaded
int error_state
Definition: error.cc:101
static std::string getenv(const std::string &name)
Definition: oct-env.cc:238
void mlock(void)
Definition: variables.cc:2014
void redraw_figure(const graphics_object &go) const
static void unload_toolkit(const std::string &name)
Definition: graphics.h:2319
base_properties & get_properties(void)
Definition: graphics.h:3377
void finalize(const graphics_object &go)
Definition: dMatrix.h:35
double get_screen_resolution(void) const
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:773
OCTINTERP_API octave_value_list Ffflush(const octave_value_list &=octave_value_list(), int=0)
std::string search_path_for_file(const std::string &path, const string_vector &names)
Definition: utils.cc:254
bool is_empty(void) const
Definition: ov.h:526
void print_figure(const graphics_object &go, const std::string &term, const std::string &file, bool mono, const std::string &debug_file) const
octave_value_list ovl(const octave_value &a0)
Definition: oct-obj.h:178
void update(const graphics_object &go, int id)
#define DEFUN_DLD(name, args_name, nargout_name, doc)
Definition: defun-dld.h:59
void set___plot_stream__(const octave_value &val)
Definition: graphics.h:4873
graphics_handle get_handle(void) const
Definition: graphics.h:3363
#define OCTAVE_CONF_EXEEXT
Definition: oct-conf.h:222
octave_value as_octave_value(void) const
Definition: oct-handle.h:72
void munlock(const std::string &nm)
Definition: variables.cc:2025
void send_quit(const octave_value &pstream) const