1 ## Copyright (C) 2007-2012 Michael Goffioul
3 ## This file is part of Octave.
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.
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.
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/>.
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.
27 ## If the handle object @var{h} is of type @var{type}, return @var{h}.
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.
35 function p = ancestor (h, type, toplevel)
37 if (nargin == 2 || nargin == 3)
38 p = cell (numel (h), 1);
45 if (ischar (toplevel) && strcmpi (toplevel, "toplevel"))
48 error ("ancestor: third argument must be \"toplevel\"");
54 if (isempty (h{nh}) || ! ishandle (h{nh}))
57 if (any (strcmpi (get (h{nh}, "type"), type)))
63 h{nh} = get (h{nh}, "Parent");
70 error ("ancestor: second argument must be a string or cell array of strings");
79 %! hf = figure ("visible", "off");
82 %! assert (ancestor (l, "axes"), gca);
83 %! assert (ancestor (l, "figure"), hf);
84 %! unwind_protect_cleanup