Mercurial > hg > octave-lyh
comparison scripts/plot/ancestor.m @ 17027:e0cd6299842c
ancestor.m: Overhaul function.
Return [] for empty input.
Move input validation to front of function.
Add %!test and %!error tests.
* scripts/plot/ancestor.m: Overhaul function. Return [] for empty input. Move
input validation to front of function. Add %!test and %!error tests.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 20 Jul 2013 21:57:31 -0700 |
parents | 5d3a684236b0 |
children | eaab03308c0b |
comparison
equal
deleted
inserted
replaced
17026:be52288f827b | 17027:e0cd6299842c |
---|---|
24 ## cell array of strings, return the first parent whose type matches | 24 ## cell array of strings, return the first parent whose type matches |
25 ## any of the given type strings. | 25 ## any of the given type strings. |
26 ## | 26 ## |
27 ## If the handle object @var{h} is of type @var{type}, return @var{h}. | 27 ## If the handle object @var{h} is of type @var{type}, return @var{h}. |
28 ## | 28 ## |
29 ## If @code{"toplevel"} is given as a 3rd argument, return the highest | 29 ## If @code{"toplevel"} is given as a third argument, return the highest |
30 ## parent in the object hierarchy that matches the condition, instead | 30 ## parent in the object hierarchy that matches the condition, instead |
31 ## of the first (nearest) one. | 31 ## of the first (nearest) one. |
32 ## @seealso{get, set} | 32 ## @seealso{findobj, findall, allchild} |
33 ## @end deftypefn | 33 ## @end deftypefn |
34 | 34 |
35 function p = ancestor (h, type, toplevel) | 35 function p = ancestor (h, type, toplevel) |
36 | 36 |
37 if (nargin == 2 || nargin == 3) | 37 if (nargin < 2 || nargin > 3) |
38 print_usage (); | |
39 endif | |
40 | |
41 if (ischar (type)) | |
42 type = { type }; | |
43 elseif (! iscellstr (type)) | |
44 error ("ancestor: TYPE must be a string or cell array of strings"); | |
45 endif | |
46 | |
47 find_first = true; | |
48 if (nargin == 3) | |
49 if (ischar (toplevel) && strcmpi (toplevel, "toplevel")) | |
50 find_first = false; | |
51 else | |
52 error ('ancestor: third argument must be "toplevel"'); | |
53 endif | |
54 endif | |
55 | |
56 if (isempty (h)) | |
57 p = []; | |
58 else | |
38 p = cell (numel (h), 1); | 59 p = cell (numel (h), 1); |
39 if (ischar (type)) | 60 h = num2cell (h); |
40 type = { type }; | 61 for nh = 1:numel (h) |
41 endif | 62 while (true) |
42 if (iscellstr (type)) | 63 if (isempty (h{nh}) || ! ishandle (h{nh})) |
43 look_first = true; | 64 break; |
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 | 65 endif |
50 endif | 66 if (any (strcmpi (get (h{nh}, "type"), type))) |
51 h = num2cell (h); | 67 p{nh} = h{nh}; |
52 for nh = 1:numel (h) | 68 if (find_first) |
53 while (true) | |
54 if (isempty (h{nh}) || ! ishandle (h{nh})) | |
55 break; | 69 break; |
56 endif | 70 endif |
57 if (any (strcmpi (get (h{nh}, "type"), type))) | 71 endif |
58 p{nh} = h{nh}; | 72 h{nh} = get (h{nh}, "parent"); |
59 if (look_first) | 73 endwhile |
60 break; | 74 endfor |
61 endif | 75 if (nh == 1) |
62 endif | 76 p = p{1}; |
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 | 77 endif |
72 else | |
73 print_usage (); | |
74 endif | 78 endif |
75 | 79 |
76 endfunction | 80 endfunction |
77 | 81 |
78 | 82 |
79 %!test | 83 %!test |
80 %! hf = figure ("visible", "off"); | 84 %! hf = figure ("visible", "off"); |
81 %! unwind_protect | 85 %! unwind_protect |
82 %! l = line; | 86 %! hl = line; |
83 %! assert (ancestor (l, "axes"), gca); | 87 %! assert (ancestor (hl, "axes"), gca); |
84 %! assert (ancestor (l, "figure"), hf); | 88 %! assert (ancestor (hl, "figure"), hf); |
85 %! unwind_protect_cleanup | 89 %! unwind_protect_cleanup |
86 %! close (hf); | 90 %! close (hf); |
87 %! end_unwind_protect | 91 %! end_unwind_protect |
88 | 92 |
93 %!test | |
94 %! hf = figure ("visible", "off"); | |
95 %! unwind_protect | |
96 %! hg1 = hggroup ("parent", gca); | |
97 %! hg2 = hggroup ("parent", hg1); | |
98 %! hl = line ("parent", hg2); | |
99 %! assert (ancestor (hl, "line"), hl); | |
100 %! assert (ancestor (hl, "axes"), gca); | |
101 %! assert (ancestor (hl, "figure"), hf); | |
102 %! assert (ancestor (hl, "hggroup"), hg2); | |
103 %! assert (ancestor (hl, "hggroup", "toplevel"), hg1); | |
104 %! assert (ancestor (hl, {"hggroup", "axes"}), hg2); | |
105 %! assert (ancestor (hl, {"hggroup", "axes"}, "toplevel"), gca); | |
106 %! unwind_protect_cleanup | |
107 %! close (hf); | |
108 %! end_unwind_protect | |
109 | |
110 %!assert (ancestor ([], "axes"), []) | |
111 | |
112 %!error ancestor () | |
113 %!error ancestor (1,2,3) | |
114 %!error <TYPE must be a string> ancestor (1,2) | |
115 %!error <third argument must be "toplevel"> ancestor (1, "axes", "foo") | |
116 |