6851
|
1 ## Copyright (C) 2007 Michael Goffioul |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {@var{parent} =} ancestor (@var{h}, @var{type}) |
|
20 ## @deftypefnx {Function File} {@var{parent} =} ancestor (@var{h}, @var{type}, 'toplevel') |
|
21 ## Return the first ancestor of handle object @var{h} whose type matches |
|
22 ## @var{type}, where @var{type} is a character string. If @var{type} is a |
|
23 ## cell array of strings, return the first parent whose type matches |
|
24 ## any of the given type strings. |
|
25 ## |
|
26 ## If the handle object @var{h} is of type @var{type}, return @var{h}. |
|
27 ## |
|
28 ## If @code{"toplevel"} is given as a 3rd argument, return the highest |
|
29 ## parent in the object hierarchy that matches the condition, instead |
|
30 ## of the first (nearest) one. |
|
31 ## @seealso{get, set} |
|
32 ## @end deftypefn |
|
33 |
|
34 function p = ancestor (h, type, toplevel) |
|
35 |
|
36 if (nargin == 2 || nargin == 3) |
|
37 p = []; |
|
38 if (ischar (type)) |
|
39 type = { type }; |
|
40 endif |
|
41 if (iscellstr (type)) |
|
42 look_first = true; |
|
43 if (nargin == 3) |
|
44 if (ischar (toplevel) && strcmp (toplevel, "toplevel")) |
|
45 look_first = false; |
|
46 else |
|
47 error ("ancestor: third argument must be \"toplevel\""); |
|
48 endif |
|
49 endif |
|
50 while (true) |
|
51 if (isempty (h) || ! ishandle (h)) |
|
52 break; |
|
53 endif |
|
54 if (any (strcmpi (get (h, "type"), type))) |
|
55 p = h; |
|
56 if (look_first) |
|
57 break; |
|
58 endif |
|
59 endif |
|
60 h = get (h, "Parent"); |
|
61 endwhile |
|
62 else |
|
63 error ("ancestor: second argument must be a string or cell array of strings"); |
|
64 endif |
|
65 else |
|
66 print_usage (); |
|
67 endif |
|
68 |
|
69 endfunction |