GNU Octave  3.8.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-2013 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 "graphics.h"
40 #include "parse.h"
41 #include "variables.h"
42 
43 // PKG_ADD: register_graphics_toolkit ("gnuplot");
44 
45 static bool toolkit_loaded = false;
46 
48 {
49 public:
51  : base_graphics_toolkit ("gnuplot") { }
52 
54 
55  bool is_valid (void) const { return true; }
56 
57  bool initialize (const graphics_object& go)
58  {
59  return go.isa ("figure");
60  }
61 
62  void finalize (const graphics_object& go)
63  {
64  if (go.isa ("figure"))
65  {
66  const figure::properties& props =
67  dynamic_cast<const figure::properties&> (go.get_properties ());
68 
69  send_quit (props.get___plot_stream__ ());
70  }
71  }
72 
73  void update (const graphics_object& go, int id)
74  {
75  if (go.isa ("figure"))
76  {
77  graphics_object obj (go);
78 
79  figure::properties& props =
80  dynamic_cast<figure::properties&> (obj.get_properties ());
81 
82  switch (id)
83  {
85  if (! props.is_visible ())
86  {
87  send_quit (props.get___plot_stream__ ());
88  props.set___plot_stream__ (Matrix ());
89  props.set___enhanced__ (false);
90  }
91  break;
92  }
93  }
94  }
95 
96  void redraw_figure (const graphics_object& go) const
97  {
98  octave_value_list args;
99  args(0) = go.get_handle ().as_octave_value ();
100  feval ("__gnuplot_drawnow__", args);
101  }
102 
103  void print_figure (const graphics_object& go, const std::string& term,
104  const std::string& file, bool mono,
105  const std::string& debug_file) const
106  {
107  octave_value_list args;
108  if (! debug_file.empty ())
109  args(4) = debug_file;
110  args(3) = mono;
111  args(2) = file;
112  args(1) = term;
113  args(0) = go.get_handle ().as_octave_value ();
114  feval ("__gnuplot_drawnow__", args);
115  }
116 
118  {
119  Matrix sz (1, 2, 0.0);
120  return sz;
121  }
122 
123  double get_screen_resolution (void) const
124  { return 72.0; }
125 
126  Matrix get_screen_size (void) const
127  { return Matrix (1, 2, 0.0); }
128 
129  void close (void)
130  {
131  if (toolkit_loaded)
132  {
133  munlock ("__init_gnuplot__");
134 
135  gtk_manager::unload_toolkit ("gnuplot");
136 
137  toolkit_loaded = false;
138  }
139  }
140 
141 private:
142 
143  void send_quit (const octave_value& pstream) const
144  {
145  if (! pstream.is_empty ())
146  {
147  octave_value_list args;
148  Matrix fids = pstream.matrix_value ();
149 
150  if (! error_state)
151  {
152  Ffputs (ovl (fids(0), "\nquit;\n"));
153 
154  Ffflush (ovl (fids(0)));
155  Fpclose (ovl (fids(0)));
156 
157  if (fids.numel () > 1)
158  {
159  Fpclose (ovl (fids(1)));
160 
161  if (fids.numel () > 2)
162  Fwaitpid (ovl (fids(2)));
163  }
164  }
165  }
166  }
167 };
168 
169 // Initialize the fltk graphics toolkit.
170 
171 DEFUN_DLD (__init_gnuplot__, , , "")
172 {
173  octave_value retval;
174 
175  if (! toolkit_loaded)
176  {
177  mlock ();
178 
181 
182  toolkit_loaded = true;
183  }
184 
185  return retval;
186 }
187