comparison scripts/plot/util/figure.m @ 19705:bf27e21f0bfb

maint: Merge default to temporary audio-gsoc branch.
author John W. Eaton <jwe@octave.org>
date Wed, 31 Dec 2014 14:59:42 -0500
parents 8c648c3a2c8f
children 0e1f5a750d00
comparison
equal deleted inserted replaced
19704:dac3191a5301 19705:bf27e21f0bfb
1 ## Copyright (C) 1996-2013 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} {} figure
21 ## @deftypefnx {Command} {} figure @var{n}
22 ## @deftypefnx {Function File} {} figure (@var{n})
23 ## @deftypefnx {Function File} {} figure (@dots{}, "@var{property}", @var{value}, @dots{})
24 ## @deftypefnx {Function File} {@var{h} =} figure (@dots{})
25 ## Create a new figure window for plotting.
26 ##
27 ## If no arguments are specified, a new figure with the next available number
28 ## is created.
29 ##
30 ## If called with an integer @var{n}, and no such numbered figure exists, then
31 ## a new figure with the specified number is created. If the figure already
32 ## exists then it is made visible and becomes the current figure for plotting.
33 ##
34 ## Multiple property-value pairs may be specified for the figure object, but
35 ## they must appear in pairs.
36 ##
37 ## The optional return value @var{h} is a graphics handle to the created figure
38 ## object.
39 ## @seealso{axes, gcf, clf, close}
40 ## @end deftypefn
41
42 ## Author: jwe, Bill Denney
43
44 function h = figure (varargin)
45
46 nargs = nargin;
47
48 if (mod (nargs, 2) == 0)
49 f = NaN;
50 init_new_figure = true;
51 else
52 arg = varargin{1};
53 if (ischar (arg))
54 arg = str2double (arg);
55 endif
56 if (isscalar (arg) && isfigure (arg))
57 f = arg;
58 init_new_figure = false;
59 varargin(1) = [];
60 nargs--;
61 elseif (isscalar (arg) && isnumeric (arg) && arg > 0 && arg == fix (arg))
62 f = arg;
63 init_new_figure = true;
64 varargin(1) = [];
65 nargs--;
66 else
67 error ("figure: N must be figure handle or figure number");
68 endif
69 endif
70
71 if (rem (nargs, 2) == 1)
72 error ("figure: PROPERTY/VALUE arguments must be in pairs");
73 endif
74
75 ## Check to see if we already have a figure on the screen. If we do,
76 ## then update it if it is different from the figure we are creating
77 ## or switching to.
78 cf = get (0, "currentfigure"); # Can't use gcf () because it calls figure()
79 if (! isempty (cf) && cf != 0)
80 if (init_new_figure || cf != f)
81 drawnow ();
82 endif
83 endif
84
85 if (init_new_figure)
86 f = __go_figure__ (f, varargin{:});
87 __add_default_menu__ (f);
88 elseif (nargs > 0)
89 set (f, varargin{:});
90 endif
91
92 set (0, "currentfigure", f);
93 ## When switching to figure N, make figure visible and on top of stack,
94 ## unless visibility is explicitly switched off
95 if (! init_new_figure && ! any (strcmpi (varargin(1:2:end), "visible")
96 && strcmpi (varargin(2:2:end), "off")))
97 set (f, "visible", "on");
98 endif
99
100 if (nargout > 0)
101 h = f;
102 endif
103
104 endfunction
105
106
107 %!test
108 %! hf = figure ("visible", "off");
109 %! unwind_protect
110 %! assert (hf, gcf);
111 %! assert (isfigure (hf));
112 %! hf2 = figure (hf, "visible", "off");
113 %! assert (hf, hf2);
114 %! assert (hf2, gcf);
115 %! assert (isfigure (hf2));
116 %! assert (get (hf2, "visible"), "off");
117 %! unwind_protect_cleanup
118 %! close (hf);
119 %! end_unwind_protect
120
121 %!error <N must be figure handle or figure number> figure ({1})
122 %!error <N must be figure handle or figure number> figure ([1 2])
123 %!error <N must be figure handle or figure number> figure (-1)
124 %!error <N must be figure handle or figure number> figure (1.5)
125