]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/close.m
update packages
[CreaPhase.git] / octave_packages / m / plot / close.m
1 ## Copyright (C) 2002-2012 John W. Eaton
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  {Command} {} close
21 ## @deftypefnx {Command} {} close (@var{n})
22 ## @deftypefnx {Command} {} close all
23 ## @deftypefnx {Command} {} close all hidden
24 ## Close figure window(s) by calling the function specified by the
25 ## @code{"closerequestfcn"} property for each figure.  By default, the
26 ## function @code{closereq} is used.
27 ## @seealso{closereq}
28 ## @end deftypefn
29
30 ## Author: jwe
31 ## 2010-05-02   PBig    allow empty argument
32
33 function retval = close (arg1, arg2)
34
35   figs = [];
36
37   if (nargin == 0)
38     ## Close current figure.  Don't use gcf because that will open a new
39     ## plot window if one doesn't exist.
40     figs = get (0, "currentfigure");
41     if (! isempty (figs) && figs == 0)
42       figs = [];
43     endif
44   elseif (nargin == 1)
45     if (ischar (arg1) && strcmpi (arg1, "all"))
46       close_all_figures (false);
47     elseif (isfigure (arg1))
48       figs = arg1;
49     elseif (isempty(arg1))
50       figs = [];
51     else
52       error ("close: expecting argument to be \"all\" or a figure handle");
53     endif
54   elseif (nargin == 2
55           && ischar (arg1) && strcmpi (arg1, "all")
56           && ischar (arg2) && strcmpi (arg2, "hidden"))
57     close_all_figures (true);
58   else
59     print_usage ();
60   endif
61
62   for h = figs
63     __go_execute_callback__ (h, "closerequestfcn");
64   endfor
65
66   if (nargout > 0)
67     retval = 1;
68   endif
69
70 endfunction
71
72 function close_all_figures (close_hidden_figs)
73
74   while (! isempty (fig = get (0, "currentfigure")))
75     ## handlevisibility = get (fig, "handlevisibility")
76     ## if (close_hidden_figs || ! strcmpi (handlevisibility, "off"))
77     close (fig);
78     ## endif
79   endwhile
80
81 endfunction
82
83 %!test
84 %! hf = figure ("visible", "off");
85 %! unwind_protect
86 %!   close (hf);
87 %!   objs = findobj ("type", "figure");
88 %!   assert (isempty (intersect (objs, hf)));
89 %! unwind_protect_cleanup
90 %!   if (isfigure (hf))
91 %!     close (hf);
92 %!   endif
93 %! end_unwind_protect