changeset 8060:09f32aac8fbc

Interface for backend switch/initialization
author John W. Eaton <jwe@octave.org>
date Tue, 26 Aug 2008 13:26:05 -0400
parents 75c99d3f97d7
children f819e8992367
files scripts/ChangeLog scripts/plot/Makefile.in scripts/plot/backend.m src/ChangeLog src/DLD-FUNCTIONS/fltk_backend.cc
diffstat 5 files changed, 136 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Aug 26 13:24:15 2008 -0400
+++ b/scripts/ChangeLog	Tue Aug 26 13:26:05 2008 -0400
@@ -1,3 +1,8 @@
+2008-08-26  Michael Goffioul  <michael.goffioul@gmail.com>
+
+	* plot/backend.m: New function to handle backend switch.
+	* plot/Makefile.in: Add it.
+
 2008-08-26  David Bateman  <dbateman@free.fr>
 
 	* plot/__area__.m: Use __next_line_color__ rather than fixed set
--- a/scripts/plot/Makefile.in	Tue Aug 26 13:24:15 2008 -0400
+++ b/scripts/plot/Makefile.in	Tue Aug 26 13:26:05 2008 -0400
@@ -75,6 +75,7 @@
   area.m \
   axes.m \
   axis.m \
+  backend.m \
   bar.m \
   barh.m \
   box.m \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/plot/backend.m	Tue Aug 26 13:26:05 2008 -0400
@@ -0,0 +1,70 @@
+## Copyright (C) 2008 Michael Goffioul
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} backend (@var{name})
+## @deftypefnx {Function File} backend (@var{hlist}, @var{name})
+## Change the default graphics backend to @var{name}. If the backend is
+## not already loaded, it is first initialized (initialization is done
+## through the execution of __init_@var{name}__).
+##
+## @var{hlist} is a list of figure handles. When given, this function
+## only switches the default backend for the corresponding figures.
+## @seealso{available_backends}
+## @end deftypefn
+
+function backend (varargin)
+
+  name = "";
+  hlist = [];
+
+  if (nargin == 1)
+    if (ischar (varargin{1}))
+      name = varargin{1};
+    else
+      error ("backend: invalid backend name");
+    endif
+  elseif (nargin == 2)
+    if (isnumeric (varargin{1}) && ischar (varargin{2}))
+      hlist = varargin{1};
+      name = varargin{2};
+    elseif (ischar (varargin{2}))
+      error ("backend: invalid handle list");
+    else
+      error ("backend: invalid backend name");
+    endif
+  else
+    print_usage ();
+  endif
+
+  if (! any (strcmp (available_backends (), name)))
+    feval (["__init_", name, "__"]);
+    if (! any (strcmp (available_backends (), name)))
+      error ("backend: backend was not correctly registered");
+    endif
+  endif
+
+  if (isempty (hlist))
+    set (0, "defaultfigure__backend__", name);
+  else
+    for h = hlist(:)'
+      set (h, "__backend__", name);
+    endfor
+  endif
+
+endfunction
--- a/src/ChangeLog	Tue Aug 26 13:24:15 2008 -0400
+++ b/src/ChangeLog	Tue Aug 26 13:26:05 2008 -0400
@@ -1,5 +1,11 @@
 2008-08-26  Michael Goffioul  <michael.goffioul@gmail.com>
 
+	* DLD_FUNCTIONS/fltk_backend.cc (F__fltk_redraw__): Make static.
+	(F__init_fltk__): Protect from mutliple invocations.
+	(F__remove_fltk__): Likewise.
+	(F__init_fltk__): Register input event hook.
+	(F__remove_fltk__): Unregister input event hook.
+
 	* DLD_FUNCTIONS/fltk_backend.cc (fltk_backend::close_figure): Remove.
 	(fltk_backend::object_destroyed, fltk_backend::property_changed): New
 	methods.
--- a/src/DLD-FUNCTIONS/fltk_backend.cc	Tue Aug 26 13:24:15 2008 -0400
+++ b/src/DLD-FUNCTIONS/fltk_backend.cc	Tue Aug 26 13:26:05 2008 -0400
@@ -24,7 +24,6 @@
 
 To initialize:
 
-  input_event_hook ("__fltk_redraw__");
   __init_fltk__ ();
   set (gcf (), "__backend__", "fltk");
   plot (randn (1e3, 1));
@@ -56,6 +55,7 @@
 #undef max
 #endif
 
+#include "cmd-edit.h"
 #include "defun-dld.h"
 #include "error.h"
 #include "gl-render.h"
@@ -754,11 +754,50 @@
 };
 
 static bool backend_registered = false;
+// give FLTK no more than 0.01 sec to do it's stuff
+static double fltk_maxtime = 1e-2;
+
+static int
+__fltk_redraw__ (void)
+{
+  if (backend_registered)
+    {
+      // we scan all figures and add those which use FLTK as a backend
+      graphics_object obj = gh_manager::get_object (0);
+      if (obj && obj.isa ("root_figure"))
+	{
+	  base_properties& props = obj.get_properties ();
+	  Matrix children = props.get_children ();
+
+	  for (octave_idx_type n = 0; n < children.numel (); n++)
+	    {
+	      graphics_object fobj = gh_manager::get_object (children (n));
+	      if (fobj && fobj.isa ("figure"))
+		{
+		  figure::properties& fp =
+		      dynamic_cast<figure::properties&> (fobj.get_properties ());
+		  if (fp.get___backend__ () == FLTK_BACKEND_NAME)
+		    figure_manager::Instance ().new_window (fp);
+		}
+	    }
+	}
+
+      Fl::wait (fltk_maxtime);
+    }
+
+  return 0;
+}
+
 // call this to init the fltk backend
 DEFUN_DLD (__init_fltk__, , , "")
 {
-  graphics_backend::register_backend (new fltk_backend);
-  backend_registered = true;
+  if (! backend_registered)
+    {
+      graphics_backend::register_backend (new fltk_backend);
+      backend_registered = true;
+      
+      command_editor::add_event_hook (__fltk_redraw__);
+    }
 
   octave_value retval;
   return retval;
@@ -768,20 +807,23 @@
 // call this to delete the fltk backend
 DEFUN_DLD (__remove_fltk__, , , "")
 {
-  figure_manager::Instance ().close_all ();
-  graphics_backend::unregister_backend (FLTK_BACKEND_NAME);
-  backend_registered = false;
+  if (backend_registered)
+    {
+      figure_manager::Instance ().close_all ();
+      graphics_backend::unregister_backend (FLTK_BACKEND_NAME);
+      backend_registered = false;
 
-  // FIXME ???
-  // give FLTK 10 seconds to wrap it up
-  Fl::wait(10);	
+      command_editor::remove_event_hook (__fltk_redraw__);
+
+      // FIXME ???
+      // give FLTK 10 seconds to wrap it up
+      Fl::wait(10);
+    }
+
   octave_value retval;
   return retval;	
 }
 
-// give FLTK no more than 0.01 sec to do it's stuff
-static double fltk_maxtime = 1e-2;
-
 // call this to delete the fltk backend
 DEFUN_DLD (__fltk_maxtime__, args, ,"")
 {
@@ -798,40 +840,6 @@
   return retval;
 }
 
-// call this from the idle_callback to refresh windows
-DEFUN_DLD (__fltk_redraw__, , ,
-  "internal function for the fltk backend")
-{
-  octave_value retval;
-
-  if (!backend_registered)
-    return retval;
-
-  // we scan all figures and add those which use FLTK as a backend
-  graphics_object obj = gh_manager::get_object (0);
-  if (obj && obj.isa ("root_figure"))
-    {
-      base_properties& props = obj.get_properties ();
-      Matrix children = props.get_children ();
-
-      for (octave_idx_type n = 0; n < children.numel (); n++)
-        {
-          graphics_object fobj = gh_manager::get_object (children (n));
-          if (fobj && fobj.isa ("figure"))
-	    {
-	      figure::properties& fp =
-		dynamic_cast<figure::properties&> (fobj.get_properties ());
-	      if (fp.get___backend__ () == FLTK_BACKEND_NAME)
-		figure_manager::Instance ().new_window (fp);
-	    }
-        }
-    }
-
-  Fl::wait (fltk_maxtime);	
-
-  return retval;	
-}
-
 #endif
 
 /*