__init_gnuplot__.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 /*
00024 
00025 To initialize:
00026 
00027   graphics_toolkit ("gnuplot");
00028   plot (randn (1e3, 1));
00029 
00030 */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035 
00036 #include "defun-dld.h"
00037 #include "error.h"
00038 #include "graphics.h"
00039 #include "parse.h"
00040 #include "variables.h"
00041 
00042 // PKG_ADD: register_graphics_toolkit ("gnuplot");
00043 
00044 static bool toolkit_loaded = false;
00045 
00046 class gnuplot_graphics_toolkit : public base_graphics_toolkit
00047 {
00048 public:
00049   gnuplot_graphics_toolkit (void)
00050       : base_graphics_toolkit ("gnuplot") { }
00051 
00052   ~gnuplot_graphics_toolkit (void) { }
00053 
00054   bool is_valid (void) const { return true; }
00055 
00056   bool initialize (const graphics_object& go)
00057     {
00058       return go.isa ("figure");
00059     }
00060 
00061   void finalize (const graphics_object& go)
00062     {
00063       if (go.isa ("figure"))
00064         {
00065           const figure::properties& props =
00066               dynamic_cast<const figure::properties&> (go.get_properties ());
00067 
00068           send_quit (props.get___plot_stream__ ());
00069         }
00070     }
00071 
00072   void update (const graphics_object& go, int id)
00073     {
00074       if (go.isa ("figure"))
00075         {
00076           graphics_object obj (go);
00077 
00078           figure::properties& props =
00079               dynamic_cast<figure::properties&> (obj.get_properties ());
00080 
00081           switch (id)
00082             {
00083             case base_properties::ID_VISIBLE:
00084               if (! props.is_visible ())
00085                 {
00086                   send_quit (props.get___plot_stream__ ());
00087                   props.set___plot_stream__ (Matrix ());
00088                   props.set___enhanced__ (false);
00089                 }
00090               break;
00091             }
00092         }
00093     }
00094 
00095   void redraw_figure (const graphics_object& go) const
00096     {
00097       octave_value_list args;
00098       args(0) = go.get_handle ().as_octave_value ();
00099       feval ("__gnuplot_drawnow__", args);
00100     }
00101 
00102   void print_figure (const graphics_object& go, const std::string& term,
00103                      const std::string& file, bool mono,
00104                      const std::string& debug_file) const
00105     {
00106       octave_value_list args;
00107       if (! debug_file.empty ())
00108         args(4) = debug_file;
00109       args(3) = mono;
00110       args(2) = file;
00111       args(1) = term;
00112       args(0) = go.get_handle ().as_octave_value ();
00113       feval ("__gnuplot_drawnow__", args);
00114     }
00115 
00116   Matrix get_canvas_size (const graphics_handle&) const
00117     {
00118       Matrix sz (1, 2, 0.0);
00119       return sz;
00120     }
00121 
00122   double get_screen_resolution (void) const
00123     { return 72.0; }
00124 
00125   Matrix get_screen_size (void) const
00126     { return Matrix (1, 2, 0.0); }
00127 
00128   void close (void)
00129   {
00130     if (toolkit_loaded)
00131       {
00132         munlock ("__init_gnuplot__");
00133 
00134         gtk_manager::unload_toolkit ("gnuplot");
00135 
00136         toolkit_loaded = false;
00137       }
00138   }
00139 
00140 private:
00141 
00142   void send_quit (const octave_value& pstream) const
00143     {
00144       if (! pstream.is_empty ())
00145         {
00146           octave_value_list args;
00147           Matrix fids = pstream.matrix_value ();
00148 
00149           if (! error_state)
00150             {
00151               args(1) = "\nquit;\n";
00152               args(0) = fids(0);
00153               feval ("fputs", args);
00154 
00155               args.resize (1);
00156               feval ("fflush", args);
00157               feval ("pclose", args);
00158 
00159               if (fids.numel () > 1)
00160                 {
00161                   args(0) = fids(1);
00162                   feval ("pclose", args);
00163 
00164                   if (fids.numel () > 2)
00165                     {
00166                       args(0) = fids(2);
00167                       feval ("waitpid", args);
00168                     }
00169                 }
00170             }
00171         }
00172     }
00173 };
00174 
00175 // Initialize the fltk graphics toolkit.
00176 
00177 DEFUN_DLD (__init_gnuplot__, , , "")
00178 {
00179   octave_value retval;
00180 
00181   if (! toolkit_loaded)
00182     {
00183       mlock ();
00184 
00185       graphics_toolkit tk (new gnuplot_graphics_toolkit ());
00186       gtk_manager::load_toolkit (tk);
00187 
00188       toolkit_loaded = true;
00189     }
00190 
00191   return retval;
00192 }
00193 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines