Mercurial > hg > octave-nkf
annotate scripts/plot/util/ancestor.m @ 20730:7d7c91ddc736 stable
Handle hggroup objects "buttondownfcn" when children are clicked (bug #45621)
* Canvas.cc (Canvas::canvasMousePressEvent): when an object is clicked and has an empty "buttondownfcn",
execute it's parents "buttondownfcn" if the parent is a hggroup.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Sun, 23 Aug 2015 21:45:49 +0200 |
parents | 777f26aa8e3e |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2007-2015 Michael Goffioul |
6851 | 2 ## |
7016 | 3 ## This file is part of Octave. |
6851 | 4 ## |
7016 | 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. | |
6851 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6851 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10661
diff
changeset
|
20 ## @deftypefn {Function File} {@var{parent} =} ancestor (@var{h}, @var{type}) |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{parent} =} ancestor (@var{h}, @var{type}, "toplevel") |
6851 | 22 ## Return the first ancestor of handle object @var{h} whose type matches |
20383
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
23 ## @var{type}, where @var{type} is a character string. |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
24 ## |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
25 ## If @var{type} is a cell array of strings, return the first parent whose |
777f26aa8e3e
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
26 ## type matches any of the given type strings. |
6851 | 27 ## |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
17027
diff
changeset
|
28 ## If the handle object @var{h} itself is of type @var{type}, return @var{h}. |
6851 | 29 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
30 ## If @qcode{"toplevel"} is given as a third argument, return the highest |
6851 | 31 ## parent in the object hierarchy that matches the condition, instead |
32 ## of the first (nearest) one. | |
17027 | 33 ## @seealso{findobj, findall, allchild} |
6851 | 34 ## @end deftypefn |
35 | |
36 function p = ancestor (h, type, toplevel) | |
37 | |
17027 | 38 if (nargin < 2 || nargin > 3) |
39 print_usage (); | |
40 endif | |
41 | |
42 if (ischar (type)) | |
43 type = { type }; | |
44 elseif (! iscellstr (type)) | |
45 error ("ancestor: TYPE must be a string or cell array of strings"); | |
46 endif | |
47 | |
48 find_first = true; | |
49 if (nargin == 3) | |
50 if (ischar (toplevel) && strcmpi (toplevel, "toplevel")) | |
51 find_first = false; | |
52 else | |
53 error ('ancestor: third argument must be "toplevel"'); | |
6851 | 54 endif |
17027 | 55 endif |
56 | |
57 if (isempty (h)) | |
58 p = []; | |
59 else | |
60 p = cell (numel (h), 1); | |
61 h = num2cell (h); | |
62 for nh = 1:numel (h) | |
63 while (true) | |
64 if (isempty (h{nh}) || ! ishandle (h{nh})) | |
65 break; | |
6851 | 66 endif |
17027 | 67 if (any (strcmpi (get (h{nh}, "type"), type))) |
68 p{nh} = h{nh}; | |
69 if (find_first) | |
6851 | 70 break; |
71 endif | |
17027 | 72 endif |
73 h{nh} = get (h{nh}, "parent"); | |
74 endwhile | |
75 endfor | |
76 if (nh == 1) | |
77 p = p{1}; | |
6851 | 78 endif |
79 endif | |
80 | |
81 endfunction | |
13096 | 82 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
83 |
13096 | 84 %!test |
13124
2ea1658ad049
Don't use explicit figure number for tests to avoid interference with any figures opened by user.
Kai Habel <kai.habel@gmx.de>
parents:
13096
diff
changeset
|
85 %! hf = figure ("visible", "off"); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
13124
diff
changeset
|
86 %! unwind_protect |
17027 | 87 %! hl = line; |
88 %! assert (ancestor (hl, "axes"), gca); | |
89 %! assert (ancestor (hl, "figure"), hf); | |
13096 | 90 %! unwind_protect_cleanup |
91 %! close (hf); | |
92 %! end_unwind_protect | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14359
diff
changeset
|
93 |
17027 | 94 %!test |
95 %! hf = figure ("visible", "off"); | |
96 %! unwind_protect | |
97 %! hg1 = hggroup ("parent", gca); | |
98 %! hg2 = hggroup ("parent", hg1); | |
99 %! hl = line ("parent", hg2); | |
100 %! assert (ancestor (hl, "line"), hl); | |
101 %! assert (ancestor (hl, "axes"), gca); | |
102 %! assert (ancestor (hl, "figure"), hf); | |
103 %! assert (ancestor (hl, "hggroup"), hg2); | |
104 %! assert (ancestor (hl, "hggroup", "toplevel"), hg1); | |
105 %! assert (ancestor (hl, {"hggroup", "axes"}), hg2); | |
106 %! assert (ancestor (hl, {"hggroup", "axes"}, "toplevel"), gca); | |
107 %! unwind_protect_cleanup | |
108 %! close (hf); | |
109 %! end_unwind_protect | |
110 | |
111 %!assert (ancestor ([], "axes"), []) | |
112 | |
113 %!error ancestor () | |
114 %!error ancestor (1,2,3) | |
115 %!error <TYPE must be a string> ancestor (1,2) | |
116 %!error <third argument must be "toplevel"> ancestor (1, "axes", "foo") | |
117 |