]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/ancestor.m
update packages
[CreaPhase.git] / octave_packages / m / plot / ancestor.m
1 ## Copyright (C) 2007-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{parent} =} ancestor (@var{h}, @var{type})
21 ## @deftypefnx {Function File} {@var{parent} =} ancestor (@var{h}, @var{type}, 'toplevel')
22 ## Return the first ancestor of handle object @var{h} whose type matches
23 ## @var{type}, where @var{type} is a character string.  If @var{type} is a
24 ## cell array of strings, return the first parent whose type matches
25 ## any of the given type strings.
26 ##
27 ## If the handle object @var{h} is of type @var{type}, return @var{h}.
28 ##
29 ## If @code{"toplevel"} is given as a 3rd argument, return the highest
30 ## parent in the object hierarchy that matches the condition, instead
31 ## of the first (nearest) one.
32 ## @seealso{get, set}
33 ## @end deftypefn
34
35 function p = ancestor (h, type, toplevel)
36
37   if (nargin == 2 || nargin == 3)
38     p = cell (numel (h), 1);
39     if (ischar (type))
40       type = { type };
41     endif
42     if (iscellstr (type))
43       look_first = true;
44       if (nargin == 3)
45         if (ischar (toplevel) && strcmpi (toplevel, "toplevel"))
46           look_first = false;
47         else
48           error ("ancestor: third argument must be \"toplevel\"");
49         endif
50       endif
51       h = num2cell (h);
52       for nh = 1:numel(h)
53         while (true)
54           if (isempty (h{nh}) || ! ishandle (h{nh}))
55             break;
56           endif
57           if (any (strcmpi (get (h{nh}, "type"), type)))
58             p{nh} = h{nh};
59             if (look_first)
60               break;
61             endif
62           endif
63           h{nh} = get (h{nh}, "Parent");
64         endwhile
65       endfor
66       if (nh == 1)
67         p = p{1};
68       endif
69     else
70       error ("ancestor: second argument must be a string or cell array of strings");
71     endif
72   else
73     print_usage ();
74   endif
75
76 endfunction
77
78 %!test
79 %! hf = figure ("visible", "off");
80 %! unwind_protect
81 %!   l = line;
82 %!   assert (ancestor (l, "axes"), gca);
83 %!   assert (ancestor (l, "figure"), hf);
84 %! unwind_protect_cleanup
85 %!   close (hf);
86 %! end_unwind_protect