]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/graphics_toolkit.m
update packages
[CreaPhase.git] / octave_packages / m / plot / graphics_toolkit.m
1 ## Copyright (C) 2008-2012 Michael Goffioul
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {@var{name} =} graphics_toolkit ()
21 ## @deftypefnx {Function File} {@var{old_name} =} graphics_toolkit (@var{name})
22 ## @deftypefnx {Function File} {} graphics_toolkit (@var{hlist}, @var{name})
23 ## Query or set the default graphics toolkit to @var{name}.  If the
24 ## toolkit is not already loaded, it is first initialized by calling the
25 ## function @code{__init_@var{name}__}.
26 ##
27 ## When called with a list of figure handles, @var{hlist}, the graphics
28 ## toolkit is changed only for the listed figures.
29 ## @seealso{available_graphics_toolkits}
30 ## @end deftypefn
31
32 function retval = graphics_toolkit (name, hlist = [])
33
34   if (nargin > 2)
35     print_usage ();
36   endif
37
38   if (nargout > 0 || nargin == 0)
39     retval = get (0, "defaultfigure__graphics_toolkit__");
40   endif
41
42   if (nargin == 0)
43     return;
44   elseif (nargin == 1)
45     if (! ischar (name))
46       error ("graphics_toolkit: invalid graphics toolkit NAME");
47     endif
48   elseif (nargin == 2)
49     ## Swap input arguments
50     [hlist, name] = deal (name, hlist);
51     if (! all (isfigure (hlist)))
52       error ("graphics_toolkit: invalid figure handle list HLIST");
53     elseif (! ischar (name))
54       error ("graphics_toolkit: invalid graphics toolkit NAME");
55     endif
56   endif
57
58   if (! any (strcmp (loaded_graphics_toolkits (), name)))
59     feval (["__init_", name, "__"]);
60     if (! any (strcmp (loaded_graphics_toolkits (), name)))
61       error ("graphics_toolkit: %s toolkit was not correctly loaded", name);
62     endif
63   endif
64
65   if (isempty (hlist))
66     set (0, "defaultfigure__graphics_toolkit__", name);
67   else
68     set (hlist, "__graphics_toolkit__", name);
69   endif
70
71 endfunction
72
73
74 %!testif HAVE_FLTK
75 %! unwind_protect
76 %!   hf = figure ("visible", "off"); 
77 %!   toolkit = graphics_toolkit ();
78 %!   assert (get (0, "defaultfigure__graphics_toolkit__"), toolkit);
79 %!   graphics_toolkit (hf, "fltk"); 
80 %!   assert (get (hf, "__graphics_toolkit__"), "fltk");
81 %! unwind_protect_cleanup
82 %!   close (hf);
83 %! end_unwind_protect
84
85 %!testif HAVE_FLTK
86 %!  old_toolkit = graphics_toolkit ();
87 %!  switch old_toolkit
88 %!    case {"gnuplot"}
89 %!      new_toolkit = "fltk";
90 %!    otherwise
91 %!      new_toolkit = "gnuplot";
92 %!  endswitch
93 %!  assert (graphics_toolkit (new_toolkit), old_toolkit)
94 %!  assert (graphics_toolkit (old_toolkit), new_toolkit)
95